Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: syntax question
- X-seq: zsh-workers 36436
- From: Joep van Delft <joepvandelft@xxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: Re: syntax question
- Date: Sun, 6 Sep 2015 21:37:33 +0200
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <20150830201324.309d36b0@ntlworld.com> <55EC6ECC.4020503__39359.859241131$1441560107$gmane$org@eastlink.ca>
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