Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: proxy name for array




On 2024-01-09 16:21, Bart Schaefer wrote:
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.

I have a lot of functions that might have to work with any of several different arrays, each with it's own name.  The guts of the function naturally needs one name to work with so that name must be a proxy for whatever the 'real' name of the function is.  My tests work ok so I can do what I want ... with eval. 

#2 - assignments don't indirect on the identifier to the left

Yes, I sorta know that. I'm not the least bit surprised all those efforts failed.

proxy_assign="${proxy}[2]"
: ${(P)proxy_assign::=TWO}

I had saved a snippet that looked much like that, but for the life of me I can't remember what '::=' does, and I didn't make a note of it at the time which is a mea culpa, and naturally it's impossible to search for '::=' in the manual or even online.  Google for: " zsh  '::=' " and get zero hits.  Man I hate that.  I'm not lazy, I try to do my own homework but there's no way of knowing where to look.

Anyway I'll try that on faith.

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.

Yeah, well I already know I'm not going to use eval if I don't have to.



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

Cool!  Pointers! That would make my issue trivial.





Messages sorted by: Reverse Date, Date, Thread, Author