zsh-users
 help / color / mirror / code / Atom feed
From: "Anthony Heading" <ajrh@ajrh.net>
To: "Bart Schaefer" <schaefer@brasslantern.com>
Cc: "Zsh Users" <zsh-users@zsh.org>
Subject: Re: indirect array element assignment?
Date: Sat, 02 Jul 2022 22:28:10 -0400	[thread overview]
Message-ID: <a6b9657e-acf2-4871-ae8e-9700dcaac780@www.fastmail.com> (raw)
In-Reply-To: <CAH+w=7YCZDE3OCFakdGfxEptnM=8zwvG3PBEH-FnSi+=oTM7pA@mail.gmail.com>

On Sat, Jul 2, 2022, at 2:13 PM, Bart Schaefer wrote:
> You can do it this way:
>
> z="${v}[3]"
> echo ${(P)z::=cherry}

Wow. Thank you!  I didn't think of that at all.

> It's not that it doesn't support subscripting, it's that it doesn't
> support parameter expansions of any kind on the left:
>
> % echo ${${grape}::=raisin}
> zsh: not an identifier:

Ah, that makes sense.  Though hmm...  there's some lhs expansion, no?
${grape[$#grape]::=raisin} for example.
Is it a necessary error by design?  I see why the (P) flag
is needed for rhs expansion because shell ${${${...}}} defines
nesting to mean something different,  but for lhs ::= I wonder
why ${${X}::=...} couldn't have well-defined natural meaning.

> There really isn't a good solution if you're trying to do array
> slices, i.e., a parenthesized list on the right of the "=".

You're referring to the typeset style solution here, maybe?
Your later solution seems to work great for all sorts of array splicing.

fruits=(apple banana carrot calamansi cherry cantaloupe durian)
# hunt down the errant vegetable and all its friends
v="fruits"
z="${v}[(r)c*,(R)c*]"
: ${(PA)z::=$signals[10,11]}
echo $fruits
% apple banana KILL USR1 durian



  parent reply	other threads:[~2022-07-03  2:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-02 10:33 Anthony Heading
2022-07-02 18:13 ` Bart Schaefer
2022-07-02 18:23   ` Bart Schaefer
2022-07-03  2:28   ` Anthony Heading [this message]
2022-07-03  3:41     ` Bart Schaefer
2022-07-03 16:43       ` Anthony Heading

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=a6b9657e-acf2-4871-ae8e-9700dcaac780@www.fastmail.com \
    --to=ajrh@ajrh.net \
    --cc=schaefer@brasslantern.com \
    --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).