zsh-users
 help / color / mirror / code / Atom feed
From: Sebastian Gniazdowski <psprint@zdharma.org>
To: Zsh Users <zsh-users@zsh.org>
Subject: When (K) hash subscript flag could be useful?
Date: Mon, 2 Oct 2017 15:44:25 +0200	[thread overview]
Message-ID: <etPan.59d242ba.35d2426e.98a8@zdharma.org> (raw)

Hello,
I was stress testing the new hash assignment syntax for dash problems (because of having regrets about not doing this before -pcre_match regression revealed itself). Didn't find problems, but incidentally I stumbled upon ${harr[(K)...]}. Completion says:

K  -- all values where subscript matched by key as pattern

I finally decoded this – it takes keys of hash, treats them as patterns, and compares to subscript. So:

% local -A harr; harr=( 1\* 2- ); typeset -p1 -- harr; print -rl "${harr[(K)1-aaa]}"
typeset -A harr=(
  ['1*']=2-
)
2-

So this is reverse to what is expected: return keys, but match subscript-query to values. However passing (k) flag to match-values (R) subscript-flag does what's expected:

% local -A harr; harr=( 1- 2- ); typeset -p1 -- harr; print -rl "${(k)harr[(R)2-*]}"
typeset -A harr=(
  [1-]=2-
)
1-

I suspect (K) was added for versality. But maybe there are know applications of such "hash = database of patterns" construct?

--  
Sebastian Gniazdowski
psprint /at/ zdharma.org


             reply	other threads:[~2017-10-02 13:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-02 13:44 Sebastian Gniazdowski [this message]
2017-10-02 16:54 ` Bart Schaefer
2017-10-02 17:51   ` Sebastian Gniazdowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=etPan.59d242ba.35d2426e.98a8@zdharma.org \
    --to=psprint@zdharma.org \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).