zsh-workers
 help / color / mirror / code / Atom feed
* issues with ${array[x][y]}
@ 2018-03-06 15:16 Stephane Chazelas
  2018-03-06 17:02 ` Bart Schaefer
  2018-03-08 19:11 ` Stephane Chazelas
  0 siblings, 2 replies; 4+ messages in thread
From: Stephane Chazelas @ 2018-03-06 15:16 UTC (permalink / raw)
  To: Zsh hackers list

$ a=(foo bar)
$ x='a[1][1]'
$ echo ${(P)x}
foo
$ echo ${a[1][1]}
f

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
$ ((a[1][1] = 2))
zsh: bad base syntax
$ typeset 'a[1][1]=2'
zsh: not an identifier: a[1][1]

See also:

$ a=(foo bar)
$ echo ${a[1][1]-a}
f
~$ echo ${a[1][1]::=a}
zsh: not an identifier: a[1][1]
$ echo ${a[1][1]:=a}
f

(see how it's happy about a[1][1] for dereferencing, but complains that it's not valid when assigning).

zsh 5.4.2 on Debian.

Same goes for associative arrays (I was initially checking
what would happen with mapfile[file][1,10]=text).

-- 
Stephane


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: issues with ${array[x][y]}
  2018-03-06 15:16 issues with ${array[x][y]} Stephane Chazelas
@ 2018-03-06 17:02 ` Bart Schaefer
  2018-03-06 21:16   ` Stephane Chazelas
  2018-03-08 19:11 ` Stephane Chazelas
  1 sibling, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2018-03-06 17:02 UTC (permalink / raw)
  To: Stephane Chazelas, Zsh hackers list

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.

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: issues with ${array[x][y]}
  2018-03-06 17:02 ` Bart Schaefer
@ 2018-03-06 21:16   ` Stephane Chazelas
  0 siblings, 0 replies; 4+ messages in thread
From: Stephane Chazelas @ 2018-03-06 21:16 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: issues with ${array[x][y]}
  2018-03-06 15:16 issues with ${array[x][y]} Stephane Chazelas
  2018-03-06 17:02 ` Bart Schaefer
@ 2018-03-08 19:11 ` Stephane Chazelas
  1 sibling, 0 replies; 4+ messages in thread
From: Stephane Chazelas @ 2018-03-08 19:11 UTC (permalink / raw)
  To: Zsh hackers list

2018-03-06 15:16:44 +0000, Stephane Chazelas:
[...]
> string[1]=x
> 
> and reference ${array[1][1]}
> 
> but:
> 
> $ a[1][1]=b
> zsh: no matches found: a[1][1]=b
> $ ((a[1][1] = 2))
> zsh: bad base syntax
> $ typeset 'a[1][1]=2'
> zsh: not an identifier: a[1][1]
[...]

Also:

$ a=(213 345)
$ echo $((a[1])) $((${a[1]}))
213 213
$ echo $((a[1][2]))
zsh: bad base syntax
$ echo $((${a[1][2]}))
1

For reference, in ksh93, ${a[1][2][3]} is used for its
multi-dimensional arrays, and

${a[1][2]}
$((a[1][2]))
a[1][2]=x
((a[1][2] = 3))
typeset -n b='a[1][2]'

are recognised consistently, but ${a[1][2]=x} is buggy:

$ ksh -c ': ${a[1][2]=x}; typeset -p a'
typeset -a a=([1]=([0]=) )
$ ksh -c 'a[1][2]=x; typeset -p a'
typeset -a a=([1]=([2]=x) )

(of course, it's more important for ksh93, as multi-dimensional
arrays would be less useful without, while zsh's a[1][2] setting
the second character of the first element of a is not going to
be often useful (except maybe for the $mapfile case mentioned
earlier))

-- 
Stephane


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-03-08 19:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-06 15:16 issues with ${array[x][y]} Stephane Chazelas
2018-03-06 17:02 ` Bart Schaefer
2018-03-06 21:16   ` Stephane Chazelas
2018-03-08 19:11 ` Stephane Chazelas

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