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

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



Thanks for the fix.

For the record, I was trying to do something like this:

separator_index=$input[(i):]
if [[ $separator_index -le $#input ]]; then
  # Found separator
  ...
fi

Currently, an empty input incorrectly triggers the conditional code. To avoid it, an extra condition for that specific case is required, which is rather annoying.

In the code above, an alternative would be to use (I) and test against 0, which works in all cases, but it feels wrong in my code where I also need the index of the first separator.

Philippe


On Fri, Jan 6, 2023 at 7:12 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
On Fri, Jan 6, 2023 at 9:24 AM Peter Stephenson
<p.w.stephenson@xxxxxxxxxxxx> wrote:
>
> > On 06/01/2023 16:01 Philippe Altherr <philippe.altherr@xxxxxxxxx> wrote:
> >
> > > s=; echo $s[(i)z];
> >
> > > 0
> >
> > Why is the last line 0 and not 1?
>
> Certainly looks plain wrong.
> None of the existing tests fail with this change.

I thought there was a discussion about this at some point in the past
... but it may have been limited to array cases.  I have the feeling
we concluded that (i) and (I) should produce the same result on empty
string for purposes of boolean tests, but I can't find such a
conversation in the archives.  I remain a little concerned that some
scripts are going to unexpectedly break, but can't point to anything,
and $anUnsetName[(i)z] returns nothing rather than 0, so it's probably
an unfounded worry.


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