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

Re: array prepend



On Jan 14,  1:36pm, Greg Klanderman wrote:
} Subject: Re: array prepend
}
} >  	VAR[]=(this is prepended)
} 
} So that's the same as VAR[1,0]=(...) ?

Well, no, because VAR[] would work regardless of KSH_ARRAYS.  But I
confess I'd forgotten about reversing the indices like that.

For those in the audience, Greg has just reminded me that you can in
fact "splice to the left" by using indices in decreasing order; and
for zsh-mode arrays there is an empty slot at zero, which you can't
reference by itself, but which is allowed in a range expression.  So
in those circumstances

    VAR[1,0]=(this is prepended)

} It doesn't really "look" very much like prepend to me.

I chose it to "look like" splicing something into an "empty" index.
If we pretend there's an empty index at each end of the array, ...

} >  	VAR[]+=(this is appended)
} >  	VAR+=(short for 'VAR[]+=')
} 
} Does VAR+=($x) differ at all from VAR+=$x if VAR is an array?

Only when $x substitutes more than one word.
 
} I was thinking of maybe '++=' for prepend, the double '+' weighting
} towards the front.

That would be OK as well.  However, it occurs to me that we could allow
VAR[] to work for associative arrays; a special case in which you *are*
permitted to set a so-called slice, because there's nothing to replace
and no order required.



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