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

Re: get keys of an associative array?

On Sun, Mar 26, 2017 at 08:34:46PM +0100, Peter Stephenson wrote:
> On Sun, 26 Mar 2017 21:18:28 +0200
> Jens Elkner <jel+zsh@xxxxxxxxxxxxxxxxxxx> wrote:
> > however, getting the key set of an array via ${!vname[@]} (as usually
> > done within ksh or bash) does not work (bad substitution).
> That's never been implemented; it may be too far away from how zsh works
> to do usefullly.

Ah ok, thought initially, can't be so hard to check, whether the first
char after a '${' is a '#' or '!' or ' ' ...  Life would be much easier ;-)
> > Couldn't find anything else, except the @k flag, but this doesn't work
> > as well, e.g.
> > 
> > unset A; typeset -A A; A['a']=1; A['b']=2; A['c']=3 ; print "${(@k)A}"
> > 
> > should print 'a b c' but prints '3'. Bug, feature? Hmmm, any hints?
> You need "${(k)A[@]}" in ksh mode, but I'm not sure why the (@) flag
> doesn't do the trick.

Ahh, yes - this works. Also found out, that if I 'setopt no_KSH_ARRAYS'
"${(@k)A}" works as well (but is inconvinient).

> As it's an associative array you get the keys
> back unordered.

Yepp. That's expected for hash sets.

Thanx a lot,
Otto-von-Guericke University     http://www.cs.uni-magdeburg.de/
Department of Computer Science   Geb. 29 R 027, Universitaetsplatz 2
39106 Magdeburg, Germany         Tel: +49 391 67 52768

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