Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
unset arbitrary associative array element
- X-seq: zsh-workers 43269
- From: Stephane Chazelas <stephane.chazelas@xxxxxxxxx>
- To: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: unset arbitrary associative array element
- Date: Sat, 11 Aug 2018 12:22:26 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mail-followup-to:mime-version :content-disposition:user-agent; bh=xb0PfUd0RuDctiji9AXP7YOqSQ5YVQpK/qDeWRlOPjU=; b=pmhHRwXwF4dHeICG1Z68DmnoL4dqnU/hNodG9tUOUupoi5jjyk5yeRWCDhuOooAzlF 2fq92UB14cyxa2vLr5iKihxiD2GogJEtB9ReKhvYRUwGFZI1C0+YK2/dGTjqTI3lcxHA zdxZgSyTMZAuudF1D9RRH9m4rrVhvlZl2FtTr3bMhnfvM64O6qVQi8NIpPlOlHdnk1S1 hfc1xrX6YilZ7mEwL+4cw1TqCJQfY7Y8RoT+MoFsZSz7itw3ujcQi4aoxdeQL/ynLjZZ 9ef9Kp27qfRWH2y8LTfiqSH93vruXdvsMZEE94cEz+YtzOx106DF9+FHQRp3qlksjXhG RpWQ==
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-unsubscribe: <mailto:zsh-workers-unsubscribe@zsh.org>
- Mail-followup-to: Zsh hackers list <zsh-workers@xxxxxxx>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
Hi,
one can set an arbitrary associative array element with:
typeset -A hash
hash[$key]=$value
But:
unset "hash[$key]"
Doesn't work for values of $key that contain "]" or "\" (or
characters whose encoding contain the bytes 0x5c or 0x5d) or for
an empty $key.
hash[$key]=()
doesn't work ("attempt to set slice of associative array"
error).
unset "hash[${key//(#m)[]\\]/\\$MATCH}]"
addresses keys with "]" and "\" but not those with characters
whose encoding contains 0x5c/5d or the empty key.
Is there any other way, other than recreating the full array
with something like:
hash=("${(@kv)hash[(I)^$key]}") # untested
ksh93 has the same kind of issues (plus various bugs for
characters that contain 0x5c/5d bytes), but supports unset
"hash[]" (even though like zsh it doesn't support hash[]=value).
In bash, one can do:
unset 'hash[$key]'
That is, the argument is evaluated as shell code! That means we
can unset arbitrary elements that way (though note that bash
doesn't support hash elements with empty keys!), but I would not
want to go there for bash as that means that things like
unset "hash[$key]"
are command injection vulnerabilities.
Not sure what's the best way to address it. Maybe like for
typeset and co, make "unset" a keyword where in
unset hash[$key]
That a[$key] is parsed the same way as in
hash[$key]=value
Or maybe a:
unset -k "$key" hash
Or
hash+=("$key") # add element without a value removes the element
In any case, it would be useful if we could set or unset the
element with the empty key with:
hash[]=value
unset "hash[]"
--
Stephane
Messages sorted by:
Reverse Date,
Date,
Thread,
Author