help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Anthony Heading <ajrh@ajrh.net>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: indirect array element assignment?
Date: Sat, 2 Jul 2022 20:41:46 -0700	[thread overview]
Message-ID: <CAH+w=7ZrmLt7i+LXYG6CiM6X1pBBaSH5HSnQwBEbGU=1Qdi0VQ@mail.gmail.com> (raw)
In-Reply-To: <a6b9657e-acf2-4871-ae8e-9700dcaac780@www.fastmail.com>

On Sat, Jul 2, 2022 at 7:28 PM Anthony Heading <ajrh@ajrh.net> wrote:
> On Sat, Jul 2, 2022, at 2:13 PM, Bart Schaefer wrote:
> > 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.

Subscripting does expansion inside the [ ], but that's not the same
context as ${${...}}.

> Is it a necessary error by design?  ... for lhs ::= I wonder
> why ${${X}::=...} couldn't have well-defined natural meaning.

For that to work, ${${X}} would have to mean the same as ${(P)X}.  As
a general rule nested expansions are processed innermost-leftmost, and
substitute rvalues rather than lvalues.  The (P) is what forces the
current level of expansion to turn into an lvalue, but that doesn't
apply to either the surrounding or the surrounded levels.

So ${(P)${X}::=thing} actually does work as long as ${X} returns a
valid identifier that can be treated as an lvalue.  Without the (P) it
remains an rvalue and you get the "not an identifier" error.

(I'm glossing over a lot of what actually goes on underneath here, but
if you're familiar with the concepts of lvalues and rvalues this is a
pretty good way to think about it.)

That does in fact mean that this works too, now that I think of it:


> > 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.

Sure, but you also have to do your own array splitting, as e.g.
${(AP)=v::=apple banana carrot}, if attempting to assign a plain list
of words.

  reply	other threads:[~2022-07-03  3:42 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
2022-07-03  3:41     ` Bart Schaefer [this message]
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:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7ZrmLt7i+LXYG6CiM6X1pBBaSH5HSnQwBEbGU=1Qdi0VQ@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=ajrh@ajrh.net \
    --cc=zsh-users@zsh.org \


* 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


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).