Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: proxy name for array
On Tue, Jan 9, 2024 at 11:00 AM Ray Andrews <rayandrews@xxxxxxxxxxx> 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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author