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

Re: issues with ${array[x][y]}



2018-03-06 09:02:31 -0800, Bart Schaefer:
> On Mar 6,  3:16pm, Stephane Chazelas wrote:
> } Subject: issues with ${array[x][y]}
> }
> } $ a=(foo bar)
> } $ x='a[1][1]'
> } $ echo ${(P)x}
> } foo
> 
> This is happening because ${{(P)var}[1]} must treat ${(P)var} as an array
> any time the name referred to by $var is an array, so array-ness is being
> preserved when doing the first a[1] dereference.

Note:

$ zsh -c 'a=(foo bar); x=a[c=1][d=2]; echo ${(P)x}, $c, $d'
foo, 1,

The [d=2] part seems to be plainly ignored, it's not as if it
was treated as temp_a=($a[1]); echo ${a[2]} which I think is
what you're implying.

More generally, it seems that anything past what is parsed as a
valid variable name or variable with subscript is ignored:

$ zsh -c 'a=x; x=a+b; echo ${(P)x}'
x


> 
> } There's also a consistency issue in that one can do:
> } 
> } string[1]=x
> } 
> } and reference ${array[1][1]}
> } 
> } but:
> } 
> } $ a[1][1]=b
> } zsh: no matches found: a[1][1]=b
> 
> Having ${array[1][1]} is mostly for convenience, because it dates from
> before ${${array[1]}[1]} would have been syntactically valid.  It was
> never intended to work as an assignment.

But would we not want that? Since we can do string[1]=x, it
seems natural that we would want to be able to do the same thing
for array elemments.

-- 
Stephane



Messages sorted by: Reverse Date, Date, Thread, Author