* Re: syntax question
[not found] ` <55EC6ECC.4020503__39359.859241131$1441560107$gmane$org@eastlink.ca>
@ 2015-09-06 19:37 ` Joep van Delft
0 siblings, 0 replies; only message in thread
From: Joep van Delft @ 2015-09-06 19:37 UTC (permalink / raw)
To: zsh-workers
On Sun, 06 Sep 2015 09:50:20 -0700
Ray Andrews <rayandrews@eastlink.ca> 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
^ permalink raw reply [flat|nested] only message in thread