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

Re: [PATCH] (take two?) typeset array[position=index]=value / unset hash[$stuff]



2021-06-01 19:51:32 -0700, Bart Schaefer:
[...]
> > The issue with the empty key seems merely to be that the subscript
> > validity test for associative arrays never changed from the one for
> > plain arrays.
> 
> To maintain error-equivalent backward compatibility I didn't "fix"
> this, instead, hash[(e)] (or hash[(e)''] if you think that more
> readable) is required in order to unset the element with the empty
> key.

I have to admit I don't see the problem here. I would have
thought allowing a[]=foo and unset 'a[]' would be no-brainers
as there's no concern about backward compatibility as those
currently return an error.

Even for plain arrays, IMO, it would make sense to allow empty
subscripts. In most contexts, an empty arithmetic expression is
interpreted as 0:

$ echo $(())
0
$ printf '%d\n' ''
0
$ set -o ksharrays; a[empty]=1; typeset -p a
typeset -a a=( 1 )

In ksh93:

$ ksh -c '(( a[] = 1 )); typeset -p a'
typeset -a a=(1)

$ ksh -c 'a[]=1'
ksh: syntax error at line 1: `[]' empty subscript
(oddly enough)

$ ksh -c 'a[""]=1; typeset -p a; unset "a[]"; typeset -p a'
typeset -a a=(1)
typeset -a a=()

$ ksh -c 'typeset -A a; a[""]=1; typeset -p a; unset "a[]"; typeset -p a'
typeset -A a=(['']=1)
typeset -A a=()

mksh:

$ mksh -xc 'a[]=1; typeset -p a; unset "a[]"; typeset -p a'
+ a[]=1
+ typeset -p a
set -A a
typeset a[0]=1
+ unset 'a[]'
+ typeset -p a


> The one compatibility issue with the foregoing is this:
[...]
> With the patch, the "(e)" appearing in the value of $bad becomes a
> subscript flag, because $bad is expanded before "unset" parses:
> % zz[$bad]=x
> % typeset -p zz
> typeset -A zz=( ['(e)bang']=x )
> % unset zz\["$bad"\]
> % typeset -p zz
> typeset -A zz=( ['(e)bang']=x )
> 
> You have to double the flag:
> % unset zz\["(e)$bad"\]

Or more legibly:

unset "zz[(e)$bad]"

> % typeset -p zz
> typeset -A zz=( )
> 
> Is that a small enough incompatibility for this to be acceptable?
[...]

Well, currently, you already need to escape the (s and )s in
general (except when they're matched):

$ key='(' zsh -c 'typeset -A a; a[$key]=x; unset "a[$key]"'
zsh:unset:1: a[(]: invalid parameter name

So I'm not sure there's much of a compatibility problem.

But while it allows unsetting the element with empty key with
unset 'a[(e)]', it seems to make it even more difficult to unset 
elements with arbitrary keys. 

One still can't use:

unset "a[$key]"

nor

unset "a[(e)$key]"

That still chokes on ()[]`\ and that still can't be worked around with

unset "a[${(b)key}]"

as it inserts backslashes in too many places and not in front of
backticks:

$ key='?' ./Src/zsh -c 'typeset -A a; a[x]=y; a[$key]=x; typeset -p a; unset "a[${(b)key}]"; typeset -p a'
typeset -A a=( ['?']=x [x]=y )
typeset -A a=( ['?']=x [x]=y )

And with (e), we can't use backslash to escape problematic
characters:

$ typeset -A a=('[' x)
$ unset 'a[(e)[]'
unset: a[(e)[]: invalid parameter name
$ unset 'a[(e)\[]'
$ typeset -p a
typeset -A a=( ['[']=x )

So, you'd need something like:

if [[ -n $key ]]; then
  () {
    set -o localoptions +o multibyte -o extendedglob
    unset "a[${key//[][()\`\\]/\\$MATCH}]"
  }
else
  unset "a[(e)]"
fi

(untested)

To unset an element with arbitrary key (granted, that's an
improvement as you can now unset the element with empty key, but
IMO not an acceptable solution).

"e" for "exact" is also a bit misleading in that case as without it,
wildcards and */@ are not treated specially.

It's also a bit confusing that subscript flags would be
seemingly parsed but later ignored (included in the value of the
key) except for (e). The fact that (e) is recognised and (ee) is
not also makes for a not very consistent API.

-- 
Stephane




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