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

Re: syntax question



On Sun, 06 Sep 2015 09:50:20 -0700
Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:

> If I have a variable who's name is partially constructed from the 
> contents of another variable:
> 
>         eval "t$bb="
>         eval "echo \$t$bb"
> 
> ... the above handles it all fine, but is there a simpler syntax?
> I understand that most/much of what 'eval' does can be written more
> elegantly.

I am not sure I understand what you are after, but you might be
looking for the P expansion flag. This says to interpret the result
of the expansion as a variable name, which will get expanded. With
this you can achieve some indirection. To quote `man 1 zshexpn`:

    For example, if you have `foo=bar' and `bar=baz', the strings
    ${(P)foo},  ${(P)${foo}}, and ${(P)$(echo bar)} will be
    expanded to `baz'.

Or are you looking for named subscripts, a.k.a. associative arrays?

   % set -A t # t is an associative array now
   % t=(key1 val1 key2 val2)
   % echo $t[key1] % val1
   % print -l ${(k)t} % prints the keys of t
   % print -l ${(v)t} % prints the values of t

Cheers,

Joep



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