From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19790 invoked from network); 29 Jul 2005 11:57:22 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 29 Jul 2005 11:57:22 -0000 Received: (qmail 68755 invoked from network); 29 Jul 2005 11:57:16 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 29 Jul 2005 11:57:16 -0000 Received: (qmail 28774 invoked by alias); 29 Jul 2005 11:57:14 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 21545 Received: (qmail 28765 invoked from network); 29 Jul 2005 11:57:14 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 29 Jul 2005 11:57:14 -0000 Received: (qmail 68502 invoked from network); 29 Jul 2005 11:57:14 -0000 Received: from mailhost1.csr.com (HELO MAILSWEEPER01.csr.com) (81.105.217.43) by a.mx.sunsite.dk with SMTP; 29 Jul 2005 11:57:07 -0000 Received: from exchange03.csr.com (unverified [10.100.137.60]) by MAILSWEEPER01.csr.com (Content Technologies SMTPRS 4.3.12) with ESMTP id for ; Fri, 29 Jul 2005 12:55:01 +0100 Received: from news01.csr.com ([10.103.143.38]) by exchange03.csr.com with Microsoft SMTPSVC(5.0.2195.6713); Fri, 29 Jul 2005 12:59:31 +0100 Received: from news01.csr.com (localhost.localdomain [127.0.0.1]) by news01.csr.com (8.13.1/8.12.11) with ESMTP id j6TBv53i008188 for ; Fri, 29 Jul 2005 12:57:06 +0100 Received: from csr.com (pws@localhost) by news01.csr.com (8.13.1/8.13.1/Submit) with ESMTP id j6TBv5Fx008185 for ; Fri, 29 Jul 2005 12:57:05 +0100 Message-Id: <200507291157.j6TBv5Fx008185@news01.csr.com> X-Authentication-Warning: news01.csr.com: pws owned process doing -bs To: zsh-workers@sunsite.dk (Zsh hackers list) Subject: Those array searching oddities again Date: Fri, 29 Jul 2005 12:57:05 +0100 From: Peter Stephenson X-OriginalArrivalTime: 29 Jul 2005 11:59:31.0426 (UTC) FILETIME=[FAA51420:01C59434] X-Spam-Checker-Version: SpamAssassin 3.0.4 (2005-06-05) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.2 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.0.4 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 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. **********************************************************************