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

proxy name for array




typeset -A body=()
LIST=( one two three four five )
body[array]=LIST
proxy=$body[array] # But name of array could change.
echo $proxy
# Comical efforts included for a laugh ... but you see what I'm trying to do:
# proxy[2]=TWO
# $~proxy[2]=TWO
# ${(P)proxy[2]}=TWO
# ${(P)proxy}[2]=TWO
# eval "${(P)proxy}[2]=TWO"
# eval "$proxy[2]=TWO"
# eval "${(P)proxy[2]}=TWO"
# eval ${(P)proxy[2]}=TWO
echo 'hoping for: one TWO three four five :-('
echo $LIST # Not changed.

... I'm not sure I've ever attempted anything like the above.  I want 'proxy' to hold an arbitrary array name and serve if place of an actual name.  '(P)' does this kind of work on the sending side of an assignment, but not on the receiving side and I'm not sure you can ever do an  expansion there anyway -- looks bloody awful.  But my (usually) false friend 'eval' works does work like this:

    eval "${proxy}[2]=TWO" # ... and having finally nailed it, it's intuitive and obvious why this works and the monstrosities above do not :-(

    echo $LIST # Done!

...  But Bart always cautions against it, so I'm wondering if there's a more kosher way.  BTW I had previously been copying entire arrays, making changes and then copying them back, but why not just use the proxy name for whatever the actual name of the array might be?  This is 'pointer think' of course. And I know we don't officially have pointers but this is a pretty good approximation.





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