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

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



On Sun, Mar 16, 2008 at 09:57:04PM +0000, Peter Stephenson wrote:
> 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.
[...]

According to the manual:

i
     Like `r', but gives the index of the match instead; this may not be
     combined with a second argument.  On the left side of an
     assignment, behaves like `r'.  For associative arrays, the key
     part of each pair is compared to the pattern, and the first
     matching key found is the result.  On failure substitutes one more
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     than the last currently valid index, as discussed under the
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     description of `r'.
     ^^^^^^^^^^^^^^^^^^^

That's why you get:

$ zsh -f -c 'a=(b); echo ${a[(i)a]}'
2

And I thought you should get 1 (1 more than the first valid
index)

$ zsh -f -c 'a=(); echo ${a[(i)a]}'
1

-- 
Stéphane



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