zsh-workers
 help / color / mirror / code / Atom feed
* Those array searching oddities again
@ 2005-07-29 11:57 Peter Stephenson
  2005-07-29 13:32 ` Peter Stephenson
  2005-08-11  6:10 ` Bart Schaefer
  0 siblings, 2 replies; 4+ messages in thread
From: Peter Stephenson @ 2005-07-29 11:57 UTC (permalink / raw)
  To: Zsh hackers list

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
far?

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

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
  fi

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).
 )
 item(tt(R))(
 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.
 )
 item(tt(i))(
 Like `tt(r)', but gives the index of the match instead; this may not be

-- 
Peter Stephenson <pws@csr.com>                  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.

**********************************************************************


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Those array searching oddities again
  2005-07-29 11:57 Those array searching oddities again Peter Stephenson
@ 2005-07-29 13:32 ` Peter Stephenson
  2005-07-29 13:38   ` DervishD
  2005-08-11  6:10 ` Bart Schaefer
  1 sibling, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2005-07-29 13:32 UTC (permalink / raw)
  To: Zsh hackers list

Peter Stephenson wrote:
> Probably there should be at least a warning in the documentation for (R)...

This is slightly more logical, using (i) and (I) instead of the
combination (k) and (r) and (R), with some cross-referencing in the
appropriate places.

This may well confuse someone reading it, but there is a quite good
reason for that...

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 13:26:33 -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((i)) flag, for
+example (assuming the option tt(KSH_ARRAYS) is not in effect):
+
+example([[ ${array[(i)pattern]} -le ${#array} ]])
+
+If tt(KSH_ARRAYS) is in effect, the tt(-le) should be replaced by tt(-lt).
 )
 item(tt(R))(
 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, this is the same as the element
+corresponding to subscript 1, although the form tt(${array[(I)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.
 )
 item(tt(i))(
 Like `tt(r)', but gives the index of the match instead; this may not be
@@ -213,10 +232,14 @@
 behaves like `tt(r)'.  For associative arrays, the key part of each pair
 is compared to the pattern, and the first matching key found is the
 result.
+
+See `tt(r)' for discussion of subscripts of failed matches.
 )
 item(tt(I))(
 Like `tt(i)', but gives the index of the last match, or all possible
 matching keys in an associative array.
+
+See `tt(R)' for discussion of subscripts of failed matches.
 )
 item(tt(k))(
 If used in a subscript on an associative array, this flag causes the keys

-- 
Peter Stephenson <pws@csr.com>                  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.

**********************************************************************


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Those array searching oddities again
  2005-07-29 13:32 ` Peter Stephenson
@ 2005-07-29 13:38   ` DervishD
  0 siblings, 0 replies; 4+ messages in thread
From: DervishD @ 2005-07-29 13:38 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh hackers list

    Hi Peter :)

 * Peter Stephenson <pws@csr.com> dixit:
> > Probably there should be at least a warning in the documentation for (R)...
> This is slightly more logical, using (i) and (I) instead of the
> combination (k) and (r) and (R), with some cross-referencing in the
> appropriate places.

    Yes, it is better understandable.
 
> This may well confuse someone reading it, but there is a quite good
> reason for that...

    I don't find it confusing, but very necessary.

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736 | http://www.dervishd.net
http://www.pleyades.net & http://www.gotesdelluna.net
It's my PC and I'll cry if I want to...


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Those array searching oddities again
  2005-07-29 11:57 Those array searching oddities again Peter Stephenson
  2005-07-29 13:32 ` Peter Stephenson
@ 2005-08-11  6:10 ` Bart Schaefer
  1 sibling, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2005-08-11  6:10 UTC (permalink / raw)
  To: Zsh hackers list

On Jul 29, 12:57pm, Peter Stephenson wrote:
}
} 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
} far?

As a matter of fact, I discussed this in zsh-users/9074 about a month ago.

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

Long wait, wasn't it?  I was out of town, just got back.

You didn't miss anything important, particularly with your second patch.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-08-11  6:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-29 11:57 Those array searching oddities again Peter Stephenson
2005-07-29 13:32 ` Peter Stephenson
2005-07-29 13:38   ` DervishD
2005-08-11  6:10 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).