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

Re: Prepend to array

On Mon, May 9, 2022 at 1:30 PM Perry Smith <pedz@xxxxxxxxxxxxxxxx> wrote:
> > fpath[1,0]=$ZDOTDIR/functions
> It does exactly what I want: it prepends $ZDOTDIR/functions to the front of the fpath array.  I can’t even find an example where the 2nd index is less than the first index.

Hm, the documentation for "Array Element Assignment" barely mentions
that you can use two comma-separated indices in an assignment at all.
It's implied by "... the element or range specified by EXP is replaced
... assigning a parenthesized list of values to an element or range
changes the number of elements in the array ..."

The documentation is also out of date with respect to "... only
single-element assignments may be made with typeset."  As of the
impending 5.9 release, it's probably also somewhat off when describing
escaping conventions for subscripts vis-a-vis associative array

> 1) Is the above syntax supported or does it just happen to work currently?

It's supported in zsh native mode, but not in ksh_arrays or
ksh_zero_subscript modes.  IIRC it was introduced because of the
subscripting flags that allow searching for a value, where a failed
search can return an index of zero.

> 2) What is the prescribed method of prepending to an array

Aside from array=(new values $array), you found it.  You can also
insert new values before any individual element X with array[X,0]=(new
values) but I don't recall if that variation is intended to remain

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