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

Re: indirect array element assignment?



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