From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 29137 invoked from network); 2 Jun 2021 10:07:08 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 2 Jun 2021 10:07:08 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20200801; t=1622628428; b=bxdSaj2VpEZ2eYLu2Fo71QPD17t65Pli1k7WT5HPMYE67bv2LbRt9hyoVLhA2PPoQ/1E9kw7/m g2gTiCWZABbx5fnnDQBsv7O1H+cyCkayAASfk6G5SUjBmsWp9vZiu0hxIqnytU8Aql9fTGg11F utZdNpvpGYdYl4ME+9oNYDOE/uadiR+Z5iHkq9lNi4paSlMu11864PmDerRJrM6stFSeBgfxN2 A/Dbb4YOxVgSorzw0MP810VkZdF6wlC/KlRlR7nvr90NpAa9v1xk8GTMMmHWAKQuLbAD4MxqAj 8s7At1VE/OoZQ5tO00Caal9JLJj/9CxToIHxyowS5iSoSA==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay5-d.mail.gandi.net) smtp.remote-ip=217.70.183.197; dmarc=none header.from=chazelas.org; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20200801; t=1622628428; bh=JNDyPyvXmT8eSEnI0Sc8lGAUtKMY1PzGDs0M6ThkCNc=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:DKIM-Signature; b=SfYmM/RpWuJe4ISbqSYWOFsLl0JO8yjXjyslgeUunvnxJbla5Z234yqcgkE5Xpyi31quZ4lN4H iWmeRYYi752lhwK+KqTQMdJm10vdOKmDTad40YizGo9V3tVdEhyrCUVpvau4Izew7Rp3ysMWap ktdrVX4Zj8/Z+ywGWH08QgHZEeLk2UH4uiW4Kk51f87/E6f8hw+z82GxkOH7KiJ0B0ivng46GK 6Dd4Nkywzxk+ek12zw17xxWec+DAcopPwn7BFnJus0e28EoavgAkEuptNih4kbhr575eMi8Mj/ WIZrtQOjzIiFFPzx2JFGM/r5rkYrx7+swqyTh1+yqk81zg==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20200801; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=5bsugYZ8rDqPbAsYFyyLbY7CoBfMqih0/eTR3cAaHxc=; b=d3716y32M8Xmpnelke2NITqLMd MiSBEXdwbgkNEqsOHw6eSWhNwLZA2PHynweRDs7O6fKIPx9ZFLqCsAFsA/tObxGWGOuQSD2u8+OwA 6m+v3KSrZiWE3FMT1Avw3wkTknkcw89jUqasfTJ/Dofs2NigITB+wt7UIEtCuNgTcru6eqCu/5tW/ L7Or80ObUdNK4/mvo4pw7osxdhjViFTlw3B6F3DIIHm/PzOBdeEov0tP0u7uUC7zFRsNlg9e4RaQ6 4kAq6KnY7ibYU5rtvaJn9JYIFUlhOj/VxyZQDOg8fSz2n4Je0NMBSXjcPJluvLKSnHCNYlZyHXXV2 rfyc/bUw==; Received: from authenticated user by zero.zsh.org with local id 1loNmF-0005lr-Dr; Wed, 02 Jun 2021 10:07:07 +0000 Authentication-Results: zsh.org; iprev=pass (relay5-d.mail.gandi.net) smtp.remote-ip=217.70.183.197; dmarc=none header.from=chazelas.org; arc=none Received: from relay5-d.mail.gandi.net ([217.70.183.197]:44341) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1loNm1-0005SP-1l; Wed, 02 Jun 2021 10:06:53 +0000 Received: (Authenticated sender: stephane@chazelas.org) by relay5-d.mail.gandi.net (Postfix) with ESMTPSA id ECC341C0008; Wed, 2 Jun 2021 10:06:51 +0000 (UTC) Date: Wed, 2 Jun 2021 11:06:51 +0100 From: Stephane Chazelas To: Bart Schaefer Cc: Zsh hackers list Subject: Re: [PATCH] (take two?) typeset array[position=index]=value / unset hash[$stuff] Message-ID: <20210602100651.qn3rrop5dup46ikc@chazelas.org> Mail-Followup-To: Bart Schaefer , Zsh hackers list References: <2ea6feb3-a686-4d83-ab27-6a582424487c@www.fastmail.com> <20200101140343.qwfx2xaojumuds3d@chaz.gmail.com> <20210430061117.buyhdhky5crqjrf2@chazelas.org> <20210505114521.bemoiekpophssbug@chazelas.org> <20210601053235.b4junj6muuwegl7b@chazelas.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Seq: 48984 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: 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