zsh-workers
 help / color / mirror / code / Atom feed
* Re:  Associative array ordering and selective unset (Re: Example function)
@ 1999-02-02  7:52 Sven Wischnowsky
  1999-02-02 11:10 ` PATCH: 3.1.5-pws-6: unset assoc[key] Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Sven Wischnowsky @ 1999-02-02  7:52 UTC (permalink / raw)
  To: zsh-workers


Bart Schaefer wrote:

> ...
> 
> We could go the ksh route and make `noglob unset foo[key]` work.  Like:
> 
> 	function unset() {
> 	    emulate -R zsh
> 	    setopt localoptions extendedglob
> 	    local arg i
> 	    for i
> 	    do
> 		arg=( "${(@s:[:)i}" )
> 		if [[ $#arg > 1 &&
> 		    $(eval print '${(t)'${arg[1]}'}') == association ]]
> 		then
> 		    eval "$arg[1]=( \${(kv)$arg[1][(I)^${(j:[:)arg[2,-1]} )"
> 		else
> 		    builtin unset $i
> 		fi
> 	    done
> 	}

Since even `vared' can now understand the subscripted syntax, I think
adding this to unset (even in C) would be a good thing. (I'm pretty
sure people will want to have a easy way to remove key/value-pairs
from associative arrays).

> Can you think of a better idiom than $(eval print '${(t)'${arg[1]}'}') to
> get the type of the variable whose name is the value of $arg[1] ?  I keep
> thinking there ought to be a way with ${(e)...} but couldn't make it work.

When implementing the new completion example stuff I was somewhat
irritated that there is no easy way to get the value of a parameter of 
which one only has the name in another parameter. Trying to get the
type of that parameter is indeed even more complicated.

Hm, `(e)' is used at the very end and I wouldn't like to change
that. Maybe yet another flag? E.g.: `P' makes the thing after the
flags be used as the name of a parameter. So `${(P)foo}' is the same
as `$foo', but `${(P)${foo}}' will take the value of `foo' as the name 
of a parameter and work on it. So your example would become:

  if [[ $#arg > 1 && ${(Pt)${argv[1]}} == association ]] ...

Looking at the code this doesn't seem too hard to implement (well, I
said `seem'...).

Bye
 Sven


--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


^ permalink raw reply	[flat|nested] 2+ messages in thread

* PATCH: 3.1.5-pws-6: unset assoc[key]
  1999-02-02  7:52 Associative array ordering and selective unset (Re: Example function) Sven Wischnowsky
@ 1999-02-02 11:10 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 1999-02-02 11:10 UTC (permalink / raw)
  To: zsh-workers

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1999-02-02 11:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-02-02  7:52 Associative array ordering and selective unset (Re: Example function) Sven Wischnowsky
1999-02-02 11:10 ` PATCH: 3.1.5-pws-6: unset assoc[key] Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).