Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: indirect array element assignment?
- X-seq: zsh-users 27862
- From: "Anthony Heading" <ajrh@xxxxxxxx>
- To: "Bart Schaefer" <schaefer@xxxxxxxxxxxxxxxx>
- Cc: "Zsh Users" <zsh-users@xxxxxxx>
- Subject: Re: indirect array element assignment?
- Date: Sat, 02 Jul 2022 22:28:10 -0400
- Archived-at: <https://zsh.org/users/27862>
- Feedback-id: i6aa94791:Fastmail
- In-reply-to: <CAH+w=7YCZDE3OCFakdGfxEptnM=8zwvG3PBEH-FnSi+=oTM7pA@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- References: <11ca04c9-1503-4cd2-a44b-635dc53e43a8@www.fastmail.com> <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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author