zsh-users
 help / color / mirror / code / Atom feed
From: Ray Andrews <rayandrews@eastlink.ca>
To: Zsh Users <zsh-users@zsh.org>
Subject: proxy name for array
Date: Tue, 9 Jan 2024 10:59:50 -0800	[thread overview]
Message-ID: <41f9b4d6-8f4a-4220-a86a-b7ccf690bd89@eastlink.ca> (raw)

[-- Attachment #1: Type: text/plain, Size: 1516 bytes --]


    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.



[-- Attachment #2: Type: text/html, Size: 2025 bytes --]

             reply	other threads:[~2024-01-09 19:22 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-09 18:59 Ray Andrews [this message]
2024-01-10  0:21 ` Bart Schaefer
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=41f9b4d6-8f4a-4220-a86a-b7ccf690bd89@eastlink.ca \
    --to=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).