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

Re: PATCH: 3.1.5 + associative arrays: keys, values, and pattern subscripts



On Dec 13,  3:33pm, Bart Schaefer wrote:
} Subject: PATCH: 3.1.5 + associative arrays: keys, values, and pattern subs
}
} Implementation notes:
} 
} I overloaded the `isarr' field of struct value to hold flag bits for all
} this, and backed out the previous hack of overloading both the `a' and `b'
} fields.  The difference between $array[*] and $array[@] is still stashed
} in the sign bit, mostly to minimize the number of lines changed.  I don't
} believe this has broken anything, but keep your eyes open.

Of course I did break something, in the course of getting [(i)pat] to NOT
return all the values in the array.

I had also messed up these cases:

                        Associative Array        Ordinary Array
                        -----------------        --------------
  $param                All values in param      All values in param
                        or empty if none         or empty if none

  $param[(i)pat]        A key in param that      Index of first value that
                        matches pattern pat      matches pattern pat

  $param[(I)pat]        All keys in param that   Index of last value that
                        match pattern pat        matches pattern pat

They worked correctly if an explicit ${(k)...} or ${(v)...} was given, but
not in the default cases.

The following patch puts things right, and must be applied *after* the
previous patch (zsh-workers/4763).

Index: Src/params.c
===================================================================
--- params.c	1998/12/14 02:17:35	1.13
+++ params.c	1998/12/14 07:27:11
@@ -751,8 +751,15 @@
 	*inv = (ind || !(v->isarr & SCANPM_WANTVALS));
     else if (v->isarr & SCANPM_WANTVALS)
 	*inv = 0;
-    else
+    else {
+	if (ind) {
+	    v->isarr |= SCANPM_WANTKEYS;
+	    v->isarr &= ~SCANPM_WANTVALS;
+	}
+	if (!down)
+	    v->isarr &= ~SCANPM_MATCHMANY;
 	*inv = ind;
+    }
 
     for (t=s, i=0; *t && ((*t != ']' && *t != Outbrack && *t != ',') || i); t++)
 	if (*t == '[' || *t == Inbrack)
@@ -1078,6 +1085,12 @@
 	if (PM_TYPE(pm->flags) & (PM_ARRAY|PM_HASHED)) {
 	    /* Overload v->isarr as the flag bits for hashed arrays. */
 	    v->isarr = flags | (isvarat ? SCANPM_ISVAR_AT : 0);
+	    /* If no flags were passed, we need something to represent *
+	     * `true' yet differ from an explicit WANTVALS.  This is a *
+	     * bit of a hack, but makes some sense:  When no subscript *
+	     * is provided, all values are substituted.                */
+	    if (!v->isarr)
+		v->isarr = SCANPM_MATCHMANY;
 	}
 	v->pm = pm;
 	v->inv = 0;


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



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