Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
When (K) hash subscript flag could be useful?
- X-seq: zsh-users 22916
- From: Sebastian Gniazdowski <psprint@xxxxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: When (K) hash subscript flag could be useful?
- Date: Mon, 2 Oct 2017 15:44:25 +0200
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
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