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

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



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