zsh-users
 help / color / mirror / code / Atom feed
* zsh array subscripting with ksh comp behaviour
@ 2014-08-21 13:54 Jerry Rocteur
  2014-08-21 14:47 ` Roman Neuhauser
  2014-08-21 19:47 ` Bart Schaefer
  0 siblings, 2 replies; 5+ messages in thread
From: Jerry Rocteur @ 2014-08-21 13:54 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 549 bytes --]

Hi,

My company gives us our default zsh in ksh compatibility mode.

I don't really like working this way so when I get a new shell I usually
just type zsh and load up my favourite options

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
] echo ${arr[2]}
two
] echo ${arr[3]}
three

setopt ksharrays

] echo ${arr[0]}
one
] echo ${arr[1]}
two
] echo ${arr[2]}
three
] echo ${arr[3]}

Regards,

Jerry Rocteur

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: zsh array subscripting with ksh comp behaviour
  2014-08-21 13:54 zsh array subscripting with ksh comp behaviour Jerry Rocteur
@ 2014-08-21 14:47 ` Roman Neuhauser
  2014-08-21 19:47 ` Bart Schaefer
  1 sibling, 0 replies; 5+ messages in thread
From: Roman Neuhauser @ 2014-08-21 14:47 UTC (permalink / raw)
  To: Jerry Rocteur; +Cc: zsh-users

# jerry.rocteur@gmail.com / 2014-08-21 15:54:45 +0200:
> 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
> ] echo ${arr[2]}
> two
> ] echo ${arr[3]}
> three
> 
> setopt ksharrays
> 
> ] echo ${arr[0]}
> one
> ] echo ${arr[1]}
> two
> ] echo ${arr[2]}
> three
> ] echo ${arr[3]}

zshparam(1):

Array Subscripts
  Individual  elements  of  an array may be selected using a subscript.
  A subscript of the form `[exp]' selects the single element exp, where
  exp is an arithmetic expression which will be subject to  arith‐ metic
  expansion  as if it were surrounded by `$((...))'.  The elements are
  numbered beginning with 1, unless the KSH_ARRAYS option is set in
  which case they are numbered from zero.

-- 
roman


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: zsh array subscripting with ksh comp behaviour
  2014-08-21 13:54 zsh array subscripting with ksh comp behaviour Jerry Rocteur
  2014-08-21 14:47 ` Roman Neuhauser
@ 2014-08-21 19:47 ` Bart Schaefer
  2014-08-23 18:03   ` Peter Stephenson
  1 sibling, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2014-08-21 19:47 UTC (permalink / raw)
  To: Zsh Users, jerry.rocteur

[-- Attachment #1: Type: text/plain, Size: 695 bytes --]

On Aug 21, 2014 6:57 AM, "Jerry Rocteur" <jerry.rocteur@gmail.com> 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.

If (b) then this has already been explained as zsh's normal 1-based
indexing (again, without ksharrays).

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: zsh array subscripting with ksh comp behaviour
  2014-08-21 19:47 ` Bart Schaefer
@ 2014-08-23 18:03   ` Peter Stephenson
  2014-08-23 19:24     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2014-08-23 18:03 UTC (permalink / raw)
  To: Zsh Users

On Thu, 21 Aug 2014 12:47:28 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Aug 21, 2014 6:57 AM, "Jerry Rocteur" <jerry.rocteur@gmail.com> 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: zsh array subscripting with ksh comp behaviour
  2014-08-23 18:03   ` Peter Stephenson
@ 2014-08-23 19:24     ` Bart Schaefer
  0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2014-08-23 19:24 UTC (permalink / raw)
  To: Zsh Users

On Aug 23,  7:03pm, Peter Stephenson wrote:
} 
} 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]}   

It's too bad that [[ -o there_is_no_such_option ]] is a critical error
rather than returning some kind of falsehood, but you can always test
in any version of the shell with

    if ( [[ -o kshzerosubscript ]] ) >&/dev/null
    then print KSH_ZERO_SUBSCRIPT
    else print not KSH_ZERO_SUBSCRIPT
    fi

Unfortunately this isn't all you need in this case, because the old
behavior occurs when EITHER (a) the option doesn't exist OR (b) the
option is set.

So you end up with something like

    if ( setopt kshzerosubscript ) >&/dev/null &&
       [[ ! -o kshzerosubscript ]]
    then print New zero subscript behavior
    else print Old zero subscript behavior
    fi


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-08-23 19:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-21 13:54 zsh array subscripting with ksh comp behaviour Jerry Rocteur
2014-08-21 14:47 ` Roman Neuhauser
2014-08-21 19:47 ` Bart Schaefer
2014-08-23 18:03   ` Peter Stephenson
2014-08-23 19:24     ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).