Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: zsh array subscripting with ksh comp behaviour
- X-seq: zsh-users 19032
- From: Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: zsh array subscripting with ksh comp behaviour
- Date: Sat, 23 Aug 2014 19:03:13 +0100
- In-reply-to: <CAH+w=7aeMrNn2M8nN65PmZHisMRA_hDY0Vs+jfLknkpJpLzEQA@mail.gmail.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CAGAAoy2-y7635O+wtC2ToocZ9_Mhze70WrrXLwFZ=4-UiC9e7w@mail.gmail.com> <CAH+w=7aeMrNn2M8nN65PmZHisMRA_hDY0Vs+jfLknkpJpLzEQA@mail.gmail.com>
On Thu, 21 Aug 2014 12:47:28 -0700
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Aug 21, 2014 6:57 AM, "Jerry Rocteur" <jerry.rocteur@xxxxxxxxx> wrote:
> >
> > I have noticed something strange, it is easy to solve but should this
> > really be default behaviour, I thought I'd report it.
> >
> > ] arr=(one two three)
> > ] echo ${arr[0]}
> > one
> > ] echo ${arr[1]}
> > one
>
> Is the behavior you find strange (a) that ${arr[0]} is "one" or (b) that
> ${arr[1]} is not "two"?
>
> If (a) you may be happy to hear that this has been changed in more recent
> zsh releases, so that (without ksharrays set) ${arr [0]} is the empty
> string, as with any other index that is outside the array bounds.
Right --- see the documentation of the option
KSH_ZERO_SUBSCRIPT
Treat use of a subscript of value zero in array or string
expressions as a reference to the first element, i.e. the element
that usually has the subscript 1. Ignored if KSH_ARRAYS is also
set.
If neither this option nor KSH_ARRAYS is set, accesses to an
element of an array or string with subscript zero return an empty
element or string, while attempts to set element zero of an array
or string are treated as an error. However, attempts to set an
otherwise valid subscript range that includes zero will succeed.
For example, if KSH_ZERO_SUBSCRIPT is not set,
array[0]=(element)
is an error, while
array[0,1]=(element)
is not and will replace the first element of the array.
This option is for compatibility with older versions of the shell
and is not recommended in new code.
Hence you can test directly for this feature. As long as the shell
isn't old enough that the special parameters used by the new completion
system are absent,
zmodload -i zsh/parameter
print ${+options[kshzerosubscript]}
pws
Messages sorted by:
Reverse Date,
Date,
Thread,
Author