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

Subscript flag (i) not working correctly on empty strings?



The following script

s=aaa; echo $s[(i)z];
s=aa; echo $s[(i)z];
s=a; echo $s[(i)z];
s=; echo $s[(i)z];

prints this

4
3
2
0

Why is the last line 0 and not 1? The documentation of (i) and (r) states that if the pattern isn't found in the string, the length of the string plus 1 is returned. I can't find anything that would explain the 0.

For arrays, (i) behaves as expected. The following script

a=(a a a); echo $a[(i)z];
a=(a a); echo $a[(i)z];
a=(a); echo $a[(i)z];
a=(); echo $a[(i)z];

prints this

4
3
2
1

Is the empty string behavior a bug or an (undocumented?) feature?

Philippe



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