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

Re: Memory leak when working with undefined associative array keys & problems with unset



[Replying to an off-list message w/Daniel's permission]

On Sep 17, 11:23pm, Daniel Shahaf wrote:
} 
} Bart Schaefer wrote on Sun, 17 Sep 2017 16:15 -0700:
} > On Sep 16,  8:57pm, Anssi Palin wrote:
} > } $ key='hello * [ world'
} > } $ typeset -A a=("$key" val)
} > } $ unset "a[$key]"
} > } unset: a[hello * [ world]: invalid parameter name
} > 
} > Hmm, when examining ${a[$key]} we enter parse_subscript() with $key
} > tokenized, but there's no way to get the tokenized string to reach
} > the same point in the unset builtin

Incidentally ksh93 has this same problem, at least as of 2012:

$ echo $KSH_VERSION
Version AJM 93u+ 2012-08-01
$ unset a["$key"]
ksh: unset: a[hello * [ world]: invalid variable name
$ unset "a[$key]"
ksh: unset: a[hello * [ world]: invalid variable name
$ unset "a['$key']"
ksh: unset: a['hello * [ world']: invalid variable name

This is probably why we've ignored this issue, to date.

} Couldn't we just change the interface and keep it a builtin?
} The point being to pass the subscript separately.

I don't know whether e.g. POSIX would squawk about more options to unset.

There's also this, now that we've added the [key]=val syntax:

torch% a=( ['hello * [ world']=(x y z) )
torch% typeset -p a                     
typeset -A a=( ['hello * [ world']='(x y z)' )
torch% a=( ['hello * [ world']=() )     
zsh: parse error near `()'

Since the empty parens there are currently a parse error, we could make
that work the way it does for plain arrays and unset the key.  However,
in ksh note there are no quotes around the parenthesized value (is that
because ksh allows hash elements to be arrays?) and the empty parens
version is permitted:

$ a=(['hello * [ world']=(x y z))
$ typeset -p a
typeset -A a=(['hello * [ world']=(x y z) )
$ a=(['hello * [ world']=() )    
$ typeset -p a
typeset -A a=(['hello * [ world']=())

So we may prefer to go with [increased] ksh compatibility here.



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