From: Bart Schaefer <schaefer@brasslantern.com>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: proxy name for array
Date: Tue, 9 Jan 2024 16:21:31 -0800 [thread overview]
Message-ID: <CAH+w=7ZvEa57ia6ac+c0=evCPwnu6V2GFodxe4UrU3pSBi26aw@mail.gmail.com> (raw)
In-Reply-To: <41f9b4d6-8f4a-4220-a86a-b7ccf690bd89@eastlink.ca>
On Tue, Jan 9, 2024 at 11:00 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> typeset -A body=()
> LIST=( one two three four five )
> body[array]=LIST
> proxy=$body[array] # But name of array could change.
This whole business with body[array] seems irrelevant to the question,
because by the end $proxy is just the name of the array and $body no
longer enters into it. If that's not true you're going to have to
explain yourself better.
> echo $proxy
> # Comical efforts included for a laugh ... but you see what I'm trying to do:
You're encountering two issues:
#1 - the (P) flag only works on identifiers (not array references) and
only for one level of indirection
#2 - assignments don't indirect on the identifier to the left
The way to do this without an eval is to make a new identifier for (P)
to munch on, and then use the syntax that embeds an assignment in a
parameter expansion to get the indirection:
proxy_assign="${proxy}[2]"
: ${(P)proxy_assign::=TWO}
> eval "${proxy}[2]=TWO" # ... and having finally nailed it, it's intuitive and obvious why this works and the monstrosities above do not :-(
>
> ... But Bart always cautions against it, so I'm wondering if there's a more kosher way.
The problem with that particular construct is that you're eval-ing TWO
as well as eval-ing ${proxy}[2], which though harmless in this example
could bite you later. Necessary quoting to account for that might not
be any simpler than using the extra parameter.
The perpetually-delayed next release will include "named references"
which change above issue #2 ...
typeset -n proxy=LIST
proxy[2]=TWO
# Now $LIST[2] is TWO
next prev parent reply other threads:[~2024-01-10 0:22 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-09 18:59 Ray Andrews
2024-01-10 0:21 ` Bart Schaefer [this message]
2024-01-10 0:54 ` Ray Andrews
2024-01-10 2:29 ` Bart Schaefer
2024-01-10 2:58 ` Ray Andrews
2024-01-10 3:05 ` Bart Schaefer
2024-01-10 13:44 ` Ray Andrews
2024-01-10 20:26 ` Ray Andrews
2024-01-10 21:11 ` Bart Schaefer
2024-01-10 21:22 ` Ray Andrews
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='CAH+w=7ZvEa57ia6ac+c0=evCPwnu6V2GFodxe4UrU3pSBi26aw@mail.gmail.com' \
--to=schaefer@brasslantern.com \
--cc=rayandrews@eastlink.ca \
--cc=zsh-users@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).