Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

When (K) hash subscript flag could be useful?



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



Messages sorted by: Reverse Date, Date, Thread, Author