Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Empty element elision and associative arrays (was Re: Slurping a file)
On 1/19/24, Mark J. Reed <markjreed@xxxxxxxxx> wrote:
> On Fri, Jan 19, 2024 at 9:58 AM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
> In fact, as far as I can tell, there's currently no good way to get from a
> value to its associated key at all. If you want the key/value pairs in
> order with current zsh, there is a ready solution: simply iterate through
> the sorted keys returned by *ko* and use them to get the associated value:
>
> for key in "${(@ko)array}"; do
> ... something with "$key" and "${array[$key]}" here ...
> done
>
> But if you instead iterate through the values (with or without *o*),
> there's no analogous way to get back to the corresponding key. Maybe that's
> the missing functionality we should look into instead: inverting an
> associative array. Of course duplicate values are troublesome in this
> regard.
Keys are unique but values aren't, so it's sort of a nonsensical
request; that said, you can do it ;).
% typeset -A foo; foo=( 1 bar 2 bar 3 quux 4 baz )
% echo ${(k)foo[(R)bar]}
1 2
--
Mikael Magnusson
Messages sorted by:
Reverse Date,
Date,
Thread,
Author