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

PATCH: Re: Subscript of negatively-subscripted array element gives incorrect result



On 25 Mar, Jonathan Hankins wrote:
> 
> I am getting different results taking a substring of the same array element 
> when I index it negatively than when I index it positively.  Here is an 
> example:

As Bart surmised, it was returning a one element array when negative
indexing was used. Fix is below:

Index: Src/params.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/params.c,v
retrieving revision 1.79
diff -u -r1.79 params.c
--- Src/params.c	18 Mar 2004 13:54:56 -0000	1.79
+++ Src/params.c	30 Mar 2004 12:09:58 -0000
@@ -1272,13 +1272,14 @@
 	    } else {
 		end = we ? we : start;
 	    }
+	    if (start != end) com = 1;
 	    if (start > 0)
 		start--;
 	    else if (start == 0 && end == 0)
 		end++;
 	    if (s == tbrack) {
 		s++;
-		if (v->isarr && start == end-1 && !com &&
+		if (v->isarr && !com &&
 		    (!(v->isarr & SCANPM_MATCHMANY) ||
 		     !(v->isarr & (SCANPM_MATCHKEY | SCANPM_MATCHVAL |
 				   SCANPM_KEYMATCH))))
Index: Test/D05array.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/D05array.ztst,v
retrieving revision 1.1
diff -u -r1.1 D05array.ztst
--- Test/D05array.ztst	2 Apr 2001 12:34:23 -0000	1.1
+++ Test/D05array.ztst	30 Mar 2004 12:09:58 -0000
@@ -3,6 +3,7 @@
 %prep
 
   foo=(a b c d e f g)
+  arr=(foo bar baz)
   mkdir array.tmp
   touch array.tmp/{1..9}
 
@@ -60,6 +61,20 @@
 0:A slice with a negative start and end
 >.b c d e f.
 
+  echo .${${arr[2]}[1]}.
+  echo .${${arr[-2]}[1]}.
+  echo .${${arr[2,2]}[1]}.
+  echo .${${arr[-2,-2]}[1]}.
+  echo .${${arr[2,-2]}[1]}. 
+  echo .${${arr[-2,2]}[1]}. 
+0:slices should return an array, elements a scalar
+>.b.
+>.b.
+>.bar.
+>.bar.
+>.bar.
+>.bar.
+
   setopt ksh_arrays
   echo .${foo[1,2]}.
   unsetopt ksh_arrays



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