From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5102 invoked by alias); 11 Aug 2018 11:22:34 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 43269 Received: (qmail 18664 invoked by uid 1010); 11 Aug 2018 11:22:34 -0000 X-Qmail-Scanner-Diagnostics: from mail-wm0-f41.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(74.125.82.41):SA:0(-1.9/5.0):. Processed in 1.328541 secs); 11 Aug 2018 11:22:34 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_PASS,T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.1 X-Envelope-From: stephane.chazelas@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mail-followup-to:mime-version :content-disposition:user-agent; bh=xb0PfUd0RuDctiji9AXP7YOqSQ5YVQpK/qDeWRlOPjU=; b=pmhHRwXwF4dHeICG1Z68DmnoL4dqnU/hNodG9tUOUupoi5jjyk5yeRWCDhuOooAzlF 2fq92UB14cyxa2vLr5iKihxiD2GogJEtB9ReKhvYRUwGFZI1C0+YK2/dGTjqTI3lcxHA zdxZgSyTMZAuudF1D9RRH9m4rrVhvlZl2FtTr3bMhnfvM64O6qVQi8NIpPlOlHdnk1S1 hfc1xrX6YilZ7mEwL+4cw1TqCJQfY7Y8RoT+MoFsZSz7itw3ujcQi4aoxdeQL/ynLjZZ 9ef9Kp27qfRWH2y8LTfiqSH93vruXdvsMZEE94cEz+YtzOx106DF9+FHQRp3qlksjXhG RpWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mail-followup-to :mime-version:content-disposition:user-agent; bh=xb0PfUd0RuDctiji9AXP7YOqSQ5YVQpK/qDeWRlOPjU=; b=Uuqa9zmV6N0jaMFrw59AEJIh/XMK8uHxzshP8if2ZVVh2gJfVlAxDDSbtcesTU9Ykf 8gEQP1AGqVDVG3NsjgFXwSse50nnx8lRF2sZKd13Rs32PyLfzKHIiqEWKFHguZoevUB5 JAFp7G8D5tqa/C0q9Z35YNTh/fnABbUo+4ZlPcca7oJMTfekB+ci88ML21eaOFs1ygFX rE+2FnJjZq8gmN1qJGgUzLkXd8m7tegLj0wQEh42joaKNhyb+bH1S6KlkmNbFnBsht6N TxYA/frSPo9W8NRDUkPbGJX/x5qbZAH7BHbEMZSDt1hmUd0Dq001n6abb933IY5zq4kL bHIQ== X-Gm-Message-State: AOUpUlEUVyO3iZ2XZVFe0IQkMz6KzgoI1BnKjS84M4thvzMe78iqrzYz KRGrw4ZiIQpyeqUwkZiYLwegGtO2ofA= X-Google-Smtp-Source: AA+uWPz6bSgxhL9NFEtx/euVeIj/754F74ukZ4xHRblOlVh2L1QOAD76M3P25x27wq3xw4eSDOYciA== X-Received: by 2002:a1c:6d94:: with SMTP id b20-v6mr3780628wmi.28.1533986548815; Sat, 11 Aug 2018 04:22:28 -0700 (PDT) Date: Sat, 11 Aug 2018 12:22:26 +0100 From: Stephane Chazelas To: Zsh hackers list Subject: unset arbitrary associative array element Message-ID: <20180811112226.tiscidxlc7edxqmg@chaz.gmail.com> Mail-Followup-To: Zsh hackers list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: NeoMutt/20171215 Hi, one can set an arbitrary associative array element with: typeset -A hash hash[$key]=$value But: unset "hash[$key]" Doesn't work for values of $key that contain "]" or "\" (or characters whose encoding contain the bytes 0x5c or 0x5d) or for an empty $key. hash[$key]=() doesn't work ("attempt to set slice of associative array" error). unset "hash[${key//(#m)[]\\]/\\$MATCH}]" addresses keys with "]" and "\" but not those with characters whose encoding contains 0x5c/5d or the empty key. Is there any other way, other than recreating the full array with something like: hash=("${(@kv)hash[(I)^$key]}") # untested ksh93 has the same kind of issues (plus various bugs for characters that contain 0x5c/5d bytes), but supports unset "hash[]" (even though like zsh it doesn't support hash[]=value). In bash, one can do: unset 'hash[$key]' That is, the argument is evaluated as shell code! That means we can unset arbitrary elements that way (though note that bash doesn't support hash elements with empty keys!), but I would not want to go there for bash as that means that things like unset "hash[$key]" are command injection vulnerabilities. Not sure what's the best way to address it. Maybe like for typeset and co, make "unset" a keyword where in unset hash[$key] That a[$key] is parsed the same way as in hash[$key]=value Or maybe a: unset -k "$key" hash Or hash+=("$key") # add element without a value removes the element In any case, it would be useful if we could set or unset the element with the empty key with: hash[]=value unset "hash[]" -- Stephane