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

Re: easy calling of associative array?



02.11.2015, 02:44, "Ray Andrews" <rayandrews@xxxxxxxxxxx>:
> test ()
> {
>      eval "baz=\$${1}[key1]"
>      echo $baz
> eval "foo=\$${1}[key2]"
>      echo $foo
> ...
> }
>
> If I want to pass the name of an associative array to a function via
> "$1" and then work with it's keys, the above succeeds but it's sure
> laborious. Is there some way to do the eval just once? So that I can
> end up doing:
>
> if [ "xyz[key1]" = '1' ]; then ...
>
> ... as I experiment, there seems to be no way of replacing 'xyz' with
> any expression that will do the job without needing eval's help every
> time, but I'll bet there's a way.

There is ${(P)} for indirect referencing:

    % foo=path
    % echo ${${(P)foo}[1]} $path[1]
    /home/zyx/.local/bin /home/zyx/.local/bin

. This is in `man zshexpn`.



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