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

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



On Feb 1, 11:48am, Sven Wischnowsky wrote:
} Subject: Re: Example function
}
} I was thinking about this... we could make the code keep a counter in
} assoc arrays, increment it whenever a new key is added and store the
} current value in the structure for this new element. Then we can treat 
} the whole thing as being sorted by `time of addition'.
} 
} Hm, does this sound like the right thing?

Almost.  Something about it doesn't seem quite right to me, but I can't
put my finger on what different behavior I'd expect.

I don't like the idea that every parameter table hash would end up with
another integer of overhead in every entry, but maybe that's not so bad.

} P.S.: Is `foo=( ${(kv)foo[(I)^key]} )' the easiest way to remove a
}       key/value-pair from an associative array?

Probably, if you have extendedglob set.  If you don't have extendedglob,
that's a moderately hard way to remove -all- the key/value pairs.

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
	}

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.

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



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