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

PATCH: 3.1.5 + associative arrays: fix to keys, values



On Dec 17, 11:17am, Peter Stephenson wrote:
} Subject: Re: PATCH: 3.1.5 + associative arrays: keys, values, and pattern 
}
} "Bart Schaefer" wrote:
} > The appended patch improves the implementation of the (kv) parameter flags
} > and their interaction with the (iIrR) subscripting flags.
} 
} I finally had a look at these and a couple seem to be giving problems.
} 
} For the tests,
} 
}   % typeset -A assoc
}   % assoc=(one eins two zwei three drei four vier five funf)
}   % print ${(k)assoc[four]}
}   0
}   % print ${assoc[(r)v*]}
}   eins

Fortunately these weren't too hard to fix.  (I knew SCANPM_WANTINDEX was
there for something.)

} The remaining cases as far as I've tested seem fine.  There is of
} course the point that the ordering is random (the `first' match of
} something could be any of them), but that's a feature of associative
} arrays which just needs to be made clear in the docs.

I hope I did so ...

}The syntax seems as good as anything I can think of.

OK.

(I did go to bed early.  Then I got up early.  Bleah.)

Index: Src/params.c
===================================================================
RCS file: /extra/cvsroot/zsh/zsh-3.1/Src/params.c,v
retrieving revision 1.17
diff -u -r1.17 params.c
--- params.c	1998/12/16 18:00:57	1.17
+++ params.c	1998/12/17 13:11:48
@@ -762,6 +762,8 @@
 	if (ind) {
 	    v->isarr |= SCANPM_WANTKEYS;
 	    v->isarr &= ~SCANPM_WANTVALS;
+	} else if (rev) {
+	    v->isarr |= SCANPM_WANTVALS;
 	}
 	if (!down)
 	    v->isarr &= ~SCANPM_MATCHMANY;
@@ -795,8 +797,9 @@
 		v->pm = createparam(s, PM_SCALAR|PM_UNSET);
 		paramtab = tht;
 	    }
-	    v->isarr = 0;
+	    v->isarr = (*inv ? SCANPM_WANTINDEX : 0);
 	    v->a = 0;
+	    *inv = 0;	/* We've already obtained the "index" (key) */
 	    *w = v->b = -1;
 	    r = isset(KSHARRAYS) ? 1 : 0;
 	} else
@@ -986,9 +989,11 @@
 	    }
 	    if (a > 0 && (isset(KSHARRAYS) || (v->pm->flags & PM_HASHED)))
 		a--;
-	    v->inv = 1;
-	    v->isarr = 0;
-	    v->a = v->b = a;
+	    if (v->isarr != SCANPM_WANTINDEX) {
+		v->inv = 1;
+		v->isarr = 0;
+		v->a = v->b = a;
+	    }
 	    if (*s == ',') {
 		zerr("invalid subscript", NULL, 0);
 		while (*s != ']' && *s != Outbrack)
Index: Src/subst.c
===================================================================
RCS file: /extra/cvsroot/zsh/zsh-3.1/Src/subst.c,v
retrieving revision 1.7
diff -u -r1.7 subst.c
--- subst.c	1998/12/15 06:58:15	1.7
+++ subst.c	1998/12/17 13:27:35
@@ -1009,7 +1009,11 @@
 	if ((isarr = v->isarr)) {
 	    /* No way to get here with v->inv != 0, so getvaluearr() *
 	     * is called by getarrvalue(); needn't test PM_HASHED.   */
-	    aval = getarrvalue(v);
+	    if (v->isarr == SCANPM_WANTINDEX) {
+		isarr = v->isarr = 0;
+		val = dupstring(v->pm->nam);
+	    } else
+		aval = getarrvalue(v);
 	} else {
 	    if (v->pm->flags & PM_ARRAY) {
 		int tmplen = arrlen(v->pm->gets.afn(v->pm));
Index: Src/zsh.h
===================================================================
RCS file: /extra/cvsroot/zsh/zsh-3.1/Src/zsh.h,v
retrieving revision 1.11
diff -u -r1.11 zsh.h
--- zsh.h	1998/12/15 20:14:15	1.11
+++ zsh.h	1998/12/17 13:14:04
@@ -915,7 +915,7 @@
 /* Flags for extracting elements of arrays and associative arrays */
 #define SCANPM_WANTVALS   (1<<0)
 #define SCANPM_WANTKEYS   (1<<1)
-#define SCANPM_WANTINDEX  (1<<2)	/* Presently unused */
+#define SCANPM_WANTINDEX  (1<<2)
 #define SCANPM_MATCHKEY   (1<<3)
 #define SCANPM_MATCHVAL   (1<<4)
 #define SCANPM_MATCHMANY  (1<<5)


-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com



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