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

Re: ${a[(i)pattern]} if a=()



Bart Schaefer wrote:
> On Mar 16, 10:20am, Bart Schaefer wrote:
> }
> } } $ zsh -f -c 'a=(); echo ${a[(i)a]}'
> } } 0
> } 
> } Hmm ... it seems it has always returned zero when the array is empty.
> } It would seem to make more sense for it to return 1, but I'm worried
> } there are other unforseen consequences.

I think it's supposed to return 0 because it didn't match.  1 indicates
it matched the first array element.

What has changed is that 0 is now always treated as being a failed
matched, instead of being kludged to 1.  This agrees exactly, unless I'm
missing something, with the original behaviour here, which is a failed
match and therefore shouldn't correspond to an index indicating a
successful match.  The fact we happen to need a genuine index later
is a separate issue; the point here is that we haven't found it by
reverse-matching the array.  I don't see the point in propagating the
inability to detect a failed match.

The original patch,

-        [[ $elt -gt $#amenu ]] && elt=
+        [[ $elt -eq 0 || $elt -gt $#amenu ]] && elt=

seems to me a much better solution.  The first test means we're off the
beginning, the second test off the end.  Being able to distinguish both
cases is a feature.

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



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