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

Re: string range between 1 and 0.



On Jul 18,  1:14pm, Tanaka Akira wrote:
} Subject: string range between 1 and 0.
}
} Hi. I wondered that $var[1,0] is expanded to first character of v.
[...]
} Is this intentional behaviour?

This is a side-effect of ksh array compatibility, believe it or not.

For quite some time now, it has been the case that $x[0] == $x[1] when
NOT emulating ksh.  This documented in section 2.1 of the FAQ, though it
does not seem to be mentioned in the actual manual.

The same code is used to interpret the second value in a subscript range,
so $x[0,0] == $x[1,1] == $x[0,1] == $x[1,0] unless you are using zero-
based arrays as in ksh.  This also means that $x[2,0] == $x[2,1] which is
the empty string either way; the only interesting thing happens when the
first of the range is 1.

} I think that $v[1,0] should be null string as $v[2,0].

It is, when ksharrays is set ...

If it weren't handled this way, then $x[anything,0] in the non-ksharrays
case would have to yield an "invalid subscript" error -- the only other
alternative is to treat $x[n,0] as $x[n,-1] which gives all the elements
from n to the end.

So, given the choices
    (1) leave it as is
    (2) treat [n,0] as [n,-1]
    (3) treat [n,0] as an error
my own preference is for (1).

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com



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