Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: ${a[(i)pattern]} if a=()
- X-seq: zsh-workers 24722
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Zsh hackers list <zsh-workers@xxxxxxxxxx>
- Subject: Re: ${a[(i)pattern]} if a=()
- Date: Sun, 16 Mar 2008 17:59:46 -0700
- In-reply-to: <20080316232337.GB4910@xxxxxxxxxxxxxxx>
- Mailing-list: contact zsh-workers-help@xxxxxxxxxx; run by ezmlm
- References: <schaefer@xxxxxxxxxxxxxxxx> <080316105924.ZM843@xxxxxxxxxxxxxxxxxxxxxx> <200803162157.m2GLv4Mw003473@xxxxxxxxxxxxxxxxxxx> <20080316232337.GB4910@xxxxxxxxxxxxxxx>
On Mar 16, 11:23pm, Stephane Chazelas wrote:
}
} > 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:
}
} matching key found is the result. On failure substitutes one more
} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
} than the last currently valid index, as discussed under the
} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
} description of `r'.
} ^^^^^^^^^^^^^^^^^^^
The reason we're getting into trouble here is because there is *no* valid
index when the array is empty. 0 is no longer a "currently valid index",
so to return "one more than the last" is in some sense also invalid, and
zero is the only index that is always invalid (barring KSH_* behavior).
My personal opinion is that Stephane has the right of it here, and this
empty-array case is just one that we missed way back when we added the
"missing (i) falls of the top end" behavior.
However, another way out may be to return -1 for (i) on an empty array.
Then it's "valid" for appending to the array, but distinguishable from
matching the first element. That would be kind of a strange edge case,
though, because the $a[(i)] < $#array test for presence of an element
would still break.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author