zsh style associative array assignment bug
Hey, When doing an assignment with an uneven number of elements bash currently silently treat the last element as a key and assigns it an empty string. $ typeset -A ary=(this feature came from zsh); typeset -p ary declare -A ary=([came]="from" [this]="feature" [zsh]="" ) In zsh this is an error, % typeset -A ary=(this feature came from zsh); typeset -p ary zsh: bad set of key/value pairs for associative array Could bash be adjusted to align with zsh in this case?
Re: zsh style associative array assignment bug
28 Mart 2021 Pazar tarihinde Eric Cook yazdı: > Hey, > > When doing an assignment with an uneven number of elements bash currently > silently treat the last element > as a key and assigns it an empty string. > > $ typeset -A ary=(this feature came from zsh); typeset -p ary > declare -A ary=([came]="from" [this]="feature" [zsh]="" ) > > In zsh this is an error, > % typeset -A ary=(this feature came from zsh); typeset -p ary > zsh: bad set of key/value pairs for associative array > > Could bash be adjusted to align with zsh in this case? Why? I think it's better this way. -- Oğuz
Re: zsh style associative array assignment bug
On 3/28/21 12:25 AM, Oğuz wrote: Why? I think it's better this way. -- Oğuz 1) For consistency sake with the shell the idea was borrowed from mostly. 2) Prior to this extension bash required specifying the key and value for AA assignments, so it seems weird to silently ignore that a value wasn't given now. 2.5) I subjectively think passing an odd number of elements to declare is more often than not to be a mistake that the user would be interested in knowing about. With the way it is now, you could save a few characters to do a seen array. $ while read -r key; ... seen+=("$key") ... but not really much else.