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

Re: Associative array ordering and selective unset (Re: Example function)



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@xxxxxxxxxxxxxxxxxxxxxxx



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