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

Those array searching oddities again

I know that searching through arrays for matches gives odd but generally
consistent results --- the indices drop off the end in the direction you
are searching --- but surely the following is going one weirdness too

  % print ${signals[(R)PatternNotFound]}

This is a consequence of the fact that as the match didn't work the
index returned is 0 (one less than the first valid index searching
backwards).  This is used as a real index and by the normal logic it
picks up array element 1.

This means that tests like

  if [[ -n ${array[(R)pattern]} ]]; then
    # pattern supposedly found

don't work.

With ksharrays, you still get 0 for the index when the match failed, but
this time it's actually a valid index, so it's even more broken: even if
you use the (k) flag, you can't tell if a reverse match succeeded.

Searching forwards with (r) does give useful results, however.  Probably
there should be at least a warning in the documentation for (R)...

I'll wait for Bart to tell me what I've missed.

Index: Doc/Zsh/params.yo
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/params.yo,v
retrieving revision 1.25
diff -u -r1.25 params.yo
--- Doc/Zsh/params.yo	1 Apr 2005 12:04:22 -0000	1.25
+++ Doc/Zsh/params.yo	29 Jul 2005 11:45:41 -0000
@@ -201,11 +201,30 @@
 possible if the parameter is not an associative array.  If the
 parameter is an associative array, only the value part of each pair is
 compared to the pattern, and the result is that value.
+If a search through an ordinary array failed, the search sets the
+subscript to one past the end of the array, and hence
+tt(${array[(r)pattern]}) will substitute the empty string.  Thus the
+success of a search can be tested by using the tt((k)) expansion flag, for
+example (assuming the option tt(KSH_ARRAYS) is not in effect):
+example([[ ${(k)array[(r)pattern]} -le ${#array} ]])
+If tt(KSH_ARRAYS) is in effect, the tt(-le) should be replaced by tt(-lt).
 Like `tt(r)', but gives the last match.  For associative arrays, gives
 all possible matches. May be used for assigning to ordinary array
 elements, but not for assigning to associative arrays.
+Note that this flag can give odd results on failure.  For an ordinary array
+the item substituted is that corresponding to subscript 0.  If the option
+tt(KSH_ARRAYS) is not in effect, the element substituted is that
+corresponding to subscript 1, although the form tt(${(k)array[(R)pattern]})
+will evaluate to 0 for a failed match.  If the option tt(KSH_ARRAYS) is in
+effect, the subscript is still 0 for a failed match; this cannot be
+distinguished from a successful match without testing tt(${array[0]})
+against the pattern.
 Like `tt(r)', but gives the index of the match instead; this may not be

Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070

This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.


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