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

Re: zsh array subscripting with ksh comp behaviour



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