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

PATCH: 3.1.5-pws-6: unset assoc[key]



On Feb 2,  8:52am, Sven Wischnowsky wrote:
} Subject: Re:  Associative array ordering and selective unset (Re: Example 
}
} 
} Bart Schaefer wrote:
} 
} > We could go the ksh route and make `noglob unset foo[key]` work.  Like:
} 
} Since even `vared' can now understand the subscripted syntax, I think
} adding this to unset (even in C) would be a good thing.

OK, then, here we go.  I've disallowed

zsh% foo=(x y z p d q)			# Ordinary array
zsh% noglob unset foo[2]
unset: foo: invalid element for unset

but it could easily be added if, for example, ksh permits it.  (It would
not, however, be all that easy to add `noglob unset foo[2,4]` -- or at
least I don't see right off how to do it.)

This patch also contains a minor optimization to the "normal" case of unset,
to avoid looking up the parameter name in the hash table twice.

Index: Doc/Zsh/builtins.yo
===================================================================
--- builtins.yo	1999/01/30 18:21:36	1.14
+++ builtins.yo	1999/02/02 11:00:18
@@ -1072,10 +1072,16 @@
 Local parameters remain local even if unset; they appear unset within scope,
 but the previous value will still reappear when the scope ends.
 
-If the tt(-m) flag is specified the
-arguments are taken as patterns (should be quoted) and all parameters
-with matching names are unset.  tt(unset -f) is equivalent to
-tt(unfunction).
+Individual elements of associative array parameters may be unset by using
+subscript syntax on var(name), which should be quoted (or the entire command
+prefixed with tt(noglob)) to protect the subscript from filename generation.
+
+If the tt(-m) flag is specified the arguments are taken as patterns (should
+be quoted) and all parameters with matching names are unset.  Note that this
+cannot be used when unsetting associative array elements, as the subscript
+will be treated as part of the pattern.
+
+tt(unset -f) is equivalent to tt(unfunction).
 )
 findex(unsetopt)
 cindex(options, unsetting)
Index: Src/builtin.c
===================================================================
--- builtin.c	1999/01/29 16:32:36	1.18
+++ builtin.c	1999/02/02 10:46:38
@@ -1928,14 +1928,39 @@
 
     /* do not glob -- unset the given parameter */
     while ((s = *argv++)) {
+	char *ss = strchr(s, '[');
+	char *sse = ss;
+	if (ss) {
+	    if (skipparens('[', ']', &sse) || *sse) {
+		zerrnam(name, "%s: invalid parameter name", s, 0);
+		returnval = 1;
+		continue;
+	    }
+	    *ss = 0;
+	}
 	pm = (Param) paramtab->getnode(paramtab, s);
 	if (!pm)
 	    returnval = 1;
 	else if ((pm->flags & PM_RESTRICTED) && isset(RESTRICTED)) {
 	    zerrnam(name, "%s: restricted", pm->nam, 0);
 	    returnval = 1;
+	} else if (ss) {
+	    if (PM_TYPE(pm->flags) == PM_HASHED) {
+		HashTable tht = paramtab;
+		if ((paramtab = pm->gets.hfn(pm))) {
+		    *--sse = 0;
+		    unsetparam(ss+1);
+		    *sse = ']';
+		}
+		paramtab = tht;
+	    } else {
+		zerrnam(name, "%s: invalid element for unset", s, 0);
+		returnval = 1;
+	    }
 	} else
-	    unsetparam(s);
+	    unsetparam_pm(pm, 0, 1);
+	if (ss)
+	    *ss = '[';
     }
     return returnval;
 }

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



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