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

Re: array prepend



>>>>> On January 17, 2013 Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:

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

right, though pretty much any script using arrays is going to break if
you flip KSH_ARRAYS.. :-)

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

yeah, I like the '[]' to indicate splicing into an "empty" index, I
was thinking VAR[]= vs VAR[]+= don't really clues me in to which is
prepend vs append.  But thinking about it more, I guess since +=
already means append, you chose to keep that meaning, so it does make
sense to me now.

> } Does VAR+=($x) differ at all from VAR+=$x if VAR is an array?

> Only when $x substitutes more than one word.

Ahh OK, I see when SHWORDSPLIT is on, VAR+=$x does not perform
splitting on $x but VAR+=($x) does.

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

You lost me there.. you can already use += on associative arrays
AFAICT, what does [] give you?  How do prepend and append differ, just
the precedence when one a new key already has a value in the
associative array?

Greg



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