Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: Sebastian Gniazdowski Newsgroups: gnu.bash.bug Subject: Feature request: alternating keys & values syntax for hashes Date: Wed, 10 Jul 2019 20:12:05 +0200 Lines: 52 Approved: bug-bash@gnu.org Message-ID: References: NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" X-Trace: usenet.stanford.edu 1562786243 19218 209.51.188.17 (10 Jul 2019 19:17:23 GMT) X-Complaints-To: action@cs.stanford.edu To: Bug-bash@gnu.org Envelope-to: Bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=YpzrlFb8FM8NXTR+z9ePJ9freNUcZXfXl/f+l3F0iBk=; b=o8LL5njrtqWjmrTkUqF3yJWjeJoZr/CwcRuBed2JcyA409kkEY1W57izx3pfuchkJn D6sqNoBD84AKp+QmalcjlnIzTFNjVS5a53ntuFdhmPJYpaVoByKLIyyke+graq0AFsqj iCNHf5qDxzvQSwKX2+8A3v7vMH4Stx5HEL42lxvNSGZqAsDenxrojonPJmzOB8i+WFAS ThAwFyixpgSRac1seXt+Qi8rT/Z67jRYXIJM1fXzMqMKiux/5842qsOq/w8oLsIT/tOo pOi6ZepUeutFcZ5/2TAQtblzIAZ/kHKI4MPkxd8348pu7CZgIYqD2KGGTr9c95sJE7lf 8NjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=YpzrlFb8FM8NXTR+z9ePJ9freNUcZXfXl/f+l3F0iBk=; b=UwisYsoj59y4qBGsi4fmiFUkiw2fVBTvtAL+jzUzrsgSVMUtvypbX2EPD+Q3oBTWBt CxW3+cMSODo7GMaq/59zg+csJi6fUSd+L/MGY4W4w6wJdx4SeWXVfpgu5nVPuf87wPib wIE3amxOmM0TBtwW0Ti9casvWQXgW8Hju2QxwcwLeEauDuCrJO1sFpQMU7M1X1v2TRGN yow8EmcjYn+vsK16eS3IBZwnN6Mq4VN8GHaM5vKXV7fyZ2eXWWLYzT6yGE9DT6qRKn+0 8y2FEWqt41/TMn5jYpujIwtOo2LwT0SsfU3yAIy2v6JwGewWu2DbrrKAMSbzJlnq/Tjr ESnQ== X-Gm-Message-State: APjAAAXcg+LnCvwGygfiao58/UdG5hHOTy3KMUzZzvSzdaTCPvgDfMNs RJfYnK4WKHUYohSWwJgBKimGn26tHMPPu8zmK9J+olMb X-Google-Smtp-Source: APXvYqxKXHVcVNssF66nfjCDKdaTbnn2qcZQQRBEr3PfWmrUO3oVEbDxMAuMjPqA/DUY4/6rymyvaCLXVeVqJj5PLhk= X-Received: by 2002:a67:8e0a:: with SMTP id q10mr18738762vsd.215.1562782336464; Wed, 10 Jul 2019 11:12:16 -0700 (PDT) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::e33 X-Mailman-Approved-At: Wed, 10 Jul 2019 15:17:21 -0400 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: Xref: csiph.com gnu.bash.bug:15128 Hello The current syntax for hashes doesn't allow interaction between arrays and hashes. The syntax that would allow it is simple dump of the keys and values alternating and acceptance of such series of values at assignment of the hash. This syntax is long used in Zsh. Example code: declare -a array=( a 1 b 2 c 3 ) declare -A hash=( ${array[@]} ) declare -p hash => typeset -A hash=( [a]=1 [b]=2 [c]=3 ) declare -a array2=( ${(kv)hash[@]} ) declare -p array2 ==> typeset -a array2=( a 1 b 2 c 3 ) This has many useful use cases e.g. when doing an advanced, fork-(i.e. also: subshell) free shell coding. Consider e.g. a fork-free serialization of an array: array=( val1 "val2*[special-chars]" ) printf -v serialized "%q " "${array[@]}" eval "deserialized=($serialized)" With the new syntax it would be possible to serialize and deserialize hashes in the same way (the *-char is the example new syntax to dump k&v alternating): declare-A hash=( [key1]=val1 ['key2*[special-chars]']=val2 ) printf -v serialized "%q " "${*hash[@]}" typeset -A deserialized_hash eval "deserialized_hash=($serialized)" This is a one particular example use case, but I'm using array<->hash interaction on a regular basis in Zsh. The syntax-extension is of most-easy kind, it actually doesn't change anything (besides the *-like addition to the var flags, i.e. the ${*hash[@]}, to dump k&v-s), it's just about allowing hashes to accept series of values, being the k&v-s alternating. Is there a chance for such an extension? PS. The *-symbol might be the right choice, as it inherits the glob meaning "match all", and this translates to the hash variable "give back all, i.e. values AND keys". -- Sebastian Gniazdowski News: https://twitter.com/ZdharmaI IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin Blog: http://zdharma.org