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

Re: PATCH: [key]=value syntax, work in progress

Peter wrote:
> (I take silence to mean "grmf why are you even bothering to do this

Not at all. Is definitely an enrichment to zsh.

> +elements must match this form or an error is genereted; likewise, if the

Note typo.

Appending has issues - seg fault with:
  arr=( 1 2 3 )
  arr+=( [5]=val )

Bash actually appends element wise with +=. Ksh doesn't. Ksh instead would
require an inner +=. Ksh behaviour on this seems far more logical to me.
  ksh$ arr+=( [0]+=X )
  bash$ arr+=( [0]=X )

A range doesn't evoke an error, (or clever multi-element assignment):
  % arr=( [1,2]=x )
  % typeset -p arr 
typeset -a arr=( x )
Note that is the first element so it must be parsing the range rather than
treating the , as the arithmentic , operator (and returning 2).

What about brace expansion:
  % arr=( [{2..3}]=x )
  zsh: bad math expression: operand expected at `{2..3}'
  bash$ arr=( [{2..3}]=x )
  bash$ typeset -p arr
  declare -a arr='([0]="[2]=x" [1]="[3]=x")'
ksh prints a syntax error.

  % arr=( [2]={a,b,c} )
  % typeset -p arr
  typeset -a arr=( '' '{a,b,c}' )
Thats the same in ksh.
Bash appears to disable the []= form as soon as it sees brace expansion.
Ksh/current zsh patch behaviour seems preferable to me.

This is interesting (syntax error in bash). Could be good to reserve this
syntax for ksh style nested assignments.
% arr=( [1]=(x y z) )
% typeset -p arr     
typeset -a arr=( '(x y z)' )

Any user deserves what they get with this but note that as a fatal error, the
shell bails out completely. Might be nice if this could be non-fatal with the
effect that the array is not assigned.

% arr=( [99999999999]=hello )
zsh: fatal error: out of memory

It would be good to make typeset -p output use this new syntax by default
for associative arrays because (without other cues like alignment) it
can be clearer which items in the list are keys and which values. Are
there certain keys that can't easily be quoted with the new syntax? If
so, typeset -p could mix the two formats once that's supported.

I checked subscript flags (along with @ and *) and it is correctly not parsing
them as subscript flags so we just get bad math expression errors - which is
good. Completion does try to complete them though. Otherwise, completion needs
adapting to work for associative array assignments. It appears to already just
work for arrays apart from relying on setopt magicequalsubst to be useful after
the equals sign. 

> OK, so bash says this is valid:
> $ array=([3]=three four [1]=one two)

Note also:
  $ array=([3]=three four [1]=one two overwrite)



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