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

Re: 'K' subscript flag with associative array?



On Tue, 1 Mar 2011, Greg Klanderman wrote:


Hi guys, what am I doing wrong here?  Shouldn't this:

 zsh% echo ${(k)functions[(K)_*]}

give all function names starting with '_'?

You want:

${functions[(I)_*]}


If I'm reading the man page right, I'd interpret your attempt as:

the list of functions whose names, when taken as patterns, match the literal string: '_*'

K treats the keys as patterns.

Most function names don't contain pattern metachars, so when taken as patterns, only match their own names.

But, for example:

$ '??' () { echo question everything }
$ print -l ${(k)functions[(K)_*]}
??
# because the key '??' matches any two characters
$ print -l ${(k)functions[(K)lj]}
??

Not sure what the utility is, though.  Something like this, I guess?

$ typeset -A featureset
$ featureset=( '*.*' hasadot 'test.*' 'test function' )
$ print -l ${featureset[(K)lkjlkjljk.ojojojoj]}
hasadot
$ print -l ${featureset[(K)test.function]}
hasadot
test function

--
Best,
Ben



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