Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: ${a[(i)pattern]} if a=()
- X-seq: zsh-workers 24721
- From: Stephane Chazelas <Stephane_Chazelas@xxxxxxxx>
- To: Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
- Subject: Re: ${a[(i)pattern]} if a=()
- Date: Sun, 16 Mar 2008 23:23:37 +0000
- Cc: Zsh hackers list <zsh-workers@xxxxxxxxxx>
- In-reply-to: <200803162157.m2GLv4Mw003473@xxxxxxxxxxxxxxxxxxx>
- Mail-followup-to: Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>, Zsh hackers list <zsh-workers@xxxxxxxxxx>
- Mailing-list: contact zsh-workers-help@xxxxxxxxxx; run by ezmlm
- References: <schaefer@xxxxxxxxxxxxxxxx> <080316105924.ZM843@xxxxxxxxxxxxxxxxxxxxxx> <200803162157.m2GLv4Mw003473@xxxxxxxxxxxxxxxxxxx>
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