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

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



On Wed, 13 Sep 2017 00:13:35 -0700
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> >  If the parameter var(name) exists and is a scalar, it is replaced by a new
> >  array.  To append to an array without changing the existing values, use
> > -the syntax:
> > +one of the following:
> >  ifzman()
> >  indent(var(name)tt(+=LPAR())var(value) ...tt(RPAR()))
> > +indent(var(name)tt(+=LPAR())tt([)var(key)tt(]=)var(value) ...tt(RPAR()))
> 
> What does "append" mean when the keys are specified?  If I have
> 
> arr=( 1 2 3 4 5 )
> 
> and I do
> 
> arr+=( 6 [2]=7 8 )
> 
> does that even make sense?  That's certainly not "appending" to arr[2].

That's an override; it will work, but as you say describing it as
"append" isn't very helpful, so some extra words are useful.  There's
prior art for this as it's how associative arrays already behave:

% typeset -A hash=(one un two deux)
% hash+=(one eins)
% print -aC2 ${(kv)hash}
one  eins
two  deux

> The other point that this raises is that in ksh "typeset -p" of an associative
> array outputs e.g.
> 
> typeset -A x=([y]=2 [z]=1)
> 
> and that's the only way to assign an associative array; if you assign without
> the [k]=v syntax the parameter converts into an ordinary array.  Is this going
> to get enforced when KSH_ARRAYS and/or KSH_TYPESET are in effect?

That's probably a good idea.

By the way, the mixed syntax is going to be a nightmare:

$ array=(* [100]=foo)

is entirely valid, which means much more work either passing through
a list with extra structure to indicate the differences or trawling
through them to fix up one way or the other, in either case with
different expansion rules applied to different elements individually.

I suppose it's not that common, so doesn't have to be particularly
efficient as long as the traditional zsh way isn't affected, so I'm
inclined to the latter, i.e. convert to the new format if we detect it
in any element. The biggest problem there is that we don't know it's an
associative array till later so it's just going to have to be a place
marker that just gets removed in that case.  And then there are cases
like

typeset -A hash
hash=(key1 [key2]=value)

Plus I'd like to avoid an extra trawl through the entire list of
arguments just to check the format.

pws



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