From: Joep van Delft <joepvandelft@xs4all.nl>
To: zsh-workers@zsh.org
Subject: Re: syntax question
Date: Sun, 6 Sep 2015 21:37:33 +0200 [thread overview]
Message-ID: <20150906213733.4ab3d5ff@xs4all.nl> (raw)
In-Reply-To: <55EC6ECC.4020503__39359.859241131$1441560107$gmane$org@eastlink.ca>
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
parent reply other threads:[~2015-09-06 19:37 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <55EC6ECC.4020503__39359.859241131$1441560107$gmane$org@eastlink.ca>]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150906213733.4ab3d5ff@xs4all.nl \
--to=joepvandelft@xs4all.nl \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).