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

Re: [PATCH] (?) typeset array[position=index]=value

2021-06-02 13:34:57 +0000, Daniel Shahaf:
> Haven't read the whole thread, so apologies if I'm missing something, but:
> Please let's not invent a reserved word that uses different variable
> expansion rules.  The sequence «hash[$key]» should mean the same thing
> everywhere.

That's the problem here.

It's already different in

typeset hash[$key]=1 # same as in assignment in recent versions
                     # of zsh where here typeset is a keyword

(( hash[$key] = 1 ))

unset hash[$key] # globbing performed.
read hash[$key]
$dryrun typeset hash[$key]=1 # here typeset not recognised as
                             # keyword

$dryrun typeset 'hash[$key]=1'
read 'hash[$key]'
let 'hash[$key] = 1' # yes, it works despite the single quotes
                     # and is actually safe it seems

unset 'hash[$key]' # unsets the element of key $key (literally)

(see also

Parsing rules are different because we are in different
contexts. For unset hash[$key] or unset hash[(e)*] to treat
those as associative array lvalue, and not globs, it would need
to be a reserved word, like typeset above.

But, yes I agree it's all very messy.

> Instead, we could have a builtin that takes two separate
> arguments, as in «foo hash $key» (and «foo hash ''» to unset the element
> whose key is the empty string).
> Makes sense?

I suggested:

unset -k "$key" hash

for that.


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