Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-25 Thread Chet Ramey
On 3/24/24 1:04 PM, Zachary Santer wrote: On Fri, Mar 22, 2024 at 11:23 AM Chet Ramey wrote: This is what you can do with @K. https://lists.gnu.org/archive/html/bug-bash/2021-08/msg00119.html Word splitting doesn't happen on the rhs of an assignment statement, so you use eval. The @K quoting

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-24 Thread Dennis Williamson
On Sun, Mar 24, 2024 at 4:04 PM Greg Wooledge wrote: > The @K (capital) transformation gives you quoted strings which need to > be eval'ed. Very Bourne-shell-ish. > > The @k (lowercase) transformation gives you a list of alternating raw > key/value strings, like what you'd expect from a Tcl comm

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-24 Thread Greg Wooledge
On Sun, Mar 24, 2024 at 03:54:10PM -0500, Dennis Williamson wrote: > The @K transform outputs key value pairs for indexed arrays as well as > associative arrays (you used the @k transform which does word splitting and > loses the k-v sequence). The @K (capital) transformation gives you quoted stri

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-24 Thread Dennis Williamson
On Sun, Mar 24, 2024 at 1:56 PM Greg Wooledge wrote: > It would be pretty reasonable to have a builtin that could take an array > name plus any number of additional argument pairs, and load those pairs > as keys/values into said array. Then you could do something like this: > > declare -A ha

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-24 Thread Lawrence Velázquez
On Sun, Mar 24, 2024, at 2:56 PM, Greg Wooledge wrote: > On Sun, Mar 24, 2024 at 01:04:38PM -0400, Zachary Santer wrote: >> On Fri, Mar 22, 2024 at 11:23 AM Chet Ramey wrote: >> > >> > This is what you can do with @K. >> > >> > https://lists.gnu.org/archive/html/bug-bash/2021-08/msg00119.html >> >

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-24 Thread Oğuz
On Sunday, March 24, 2024, Oğuz wrote: > > $ printf '%s\0' "${a[@]@k}" | xargs -0 jq --args -n '$ARGS.positional > | _nwise(2) | {(.[0]): .[1]}' | jq -s add > On reflection, splitting by NULs in JQ is better; there is no guarantee that xargs will always provide an even number of arguments.

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-24 Thread Oğuz
On Sunday, March 24, 2024, Greg Wooledge wrote: > Conceptually that looks great, but how do you avoid "Argument list > too long" with larger inputs? $ declare -A a=([x]=1 [y]=2) $ printf '%s\0' "${a[@]@k}" | xargs -0 jq --args -n '$ARGS.positional | _nwise(2) | {(.[0]): .[1]}' | jq -s a

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-24 Thread Greg Wooledge
On Sun, Mar 24, 2024 at 07:46:46PM +0200, Oğuz wrote: > On Sunday, March 24, 2024, Zachary Santer wrote: > > > > Yeah, but what can you do with @k? > > > It helps when reconstructing an associative array as a JSON object in JQ > > $ declare -A a=([x]=1 [y]=2) > $ jq --args -n '[$ARGS.po

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-24 Thread Greg Wooledge
On Sun, Mar 24, 2024 at 01:04:38PM -0400, Zachary Santer wrote: > On Fri, Mar 22, 2024 at 11:23 AM Chet Ramey wrote: > > > > This is what you can do with @K. > > > > https://lists.gnu.org/archive/html/bug-bash/2021-08/msg00119.html > > > > Word splitting doesn't happen on the rhs of an assignment

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-24 Thread Oğuz
On Sunday, March 24, 2024, Zachary Santer wrote: > > Yeah, but what can you do with @k? It helps when reconstructing an associative array as a JSON object in JQ $ declare -A a=([x]=1 [y]=2) $ jq --args -n '[$ARGS.positional | _nwise(2) | {(.[0]): .[1]}] | add' "${a[@]@k}" { "y

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-24 Thread Zachary Santer
On Fri, Mar 22, 2024 at 11:23 AM Chet Ramey wrote: > > This is what you can do with @K. > > https://lists.gnu.org/archive/html/bug-bash/2021-08/msg00119.html > > Word splitting doesn't happen on the rhs of an assignment statement, so you > use eval. The @K quoting is eval-safe. Yeah, but what can

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-22 Thread Greg Wooledge
On Fri, Mar 22, 2024 at 02:09:23PM -0400, Lawrence Velázquez wrote: > On Fri, Mar 22, 2024, at 12:54 PM, Greg Wooledge wrote: > > Also, I don't see the lower-case k transformation in the man page. > > It's at the end of the list: > > https://git.savannah.gnu.org/cgit/bash.git/tree/doc/bash.1?id=f

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-22 Thread Lawrence Velázquez
On Fri, Mar 22, 2024, at 12:54 PM, Greg Wooledge wrote: > Also, I don't see the lower-case k transformation in the man page. It's at the end of the list: https://git.savannah.gnu.org/cgit/bash.git/tree/doc/bash.1?id=f3b6bd1#n3525 -- vq

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-22 Thread Greg Wooledge
On Fri, Mar 22, 2024 at 11:23:35AM -0400, Chet Ramey wrote: > This is what you can do with @K. > > https://lists.gnu.org/archive/html/bug-bash/2021-08/msg00119.html > > Word splitting doesn't happen on the rhs of an assignment statement, so you > use eval. The @K quoting is eval-safe. It would b

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-22 Thread Chet Ramey
On 3/20/24 10:16 AM, Zachary Santer wrote: and then uses eval in his examples of how Bash could incorporate similar behavior: array=( val1 "val2*[special-chars]" ) printf -v serialized "%q " "${array[@]}" eval "deserialized=($serialized)" declare-A hash=( [key1]=val1 ['key2*[special-chars]'

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-20 Thread Zachary Santer
On Wed, Mar 20, 2024 at 9:32 AM Lawrence Velázquez wrote: > > https://lists.gnu.org/archive/html/bug-bash/2019-07/msg00056.html Wherein he shows that Zsh can do this without eval: > declare -a array=( a 1 b 2 c 3 ) > declare -A hash=( ${array[@]} ) > declare -p hash > => typeset -A hash=( [a]=1

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-20 Thread Lawrence Velázquez
On Wed, Mar 20, 2024, at 7:11 AM, Zachary Santer wrote: > On Wed, Mar 20, 2024 at 12:29 AM Lawrence Velázquez wrote: >> >> This isn't specific to ${var[@]@k}. >> >> $ kv1='a 1 b 2 c 3' >> $ kv2=(a 1 b 2 c 3) >> $ declare -A aa1=($kv1) aa2=(${kv2[@]}) aa3=("${kv2[@]}") >>

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-20 Thread alex xmb sw ratchev
On Wed, Mar 20, 2024, 12:59 Greg Wooledge wrote: > On Wed, Mar 20, 2024 at 12:53:07PM +0100, alex xmb sw ratchev wrote: > > On Wed, Mar 20, 2024, 12:49 Greg Wooledge wrote: > > > > > On Wed, Mar 20, 2024 at 07:11:34AM -0400, Zachary Santer wrote: > > > > On Wed, Mar 20, 2024 at 12:29 AM Lawrence

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-20 Thread Léa Gris
Le 20/03/2024 à 12:59, Greg Wooledge écrivait : s to that , simply declare -p and run that back That will work in some cases, yes. The problem is that it locks you in to having the same array name and the same attributes (if any) as the original array had. It also runs "declare" which can cre

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-20 Thread Greg Wooledge
On Wed, Mar 20, 2024 at 12:53:07PM +0100, alex xmb sw ratchev wrote: > On Wed, Mar 20, 2024, 12:49 Greg Wooledge wrote: > > > On Wed, Mar 20, 2024 at 07:11:34AM -0400, Zachary Santer wrote: > > > On Wed, Mar 20, 2024 at 12:29 AM Lawrence Velázquez > > wrote: > > > > A couple of previous discussi

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-20 Thread alex xmb sw ratchev
On Wed, Mar 20, 2024, 12:49 Greg Wooledge wrote: > On Wed, Mar 20, 2024 at 07:11:34AM -0400, Zachary Santer wrote: > > On Wed, Mar 20, 2024 at 12:29 AM Lawrence Velázquez > wrote: > > > A couple of previous discussions: > > > - https://lists.gnu.org/archive/html/bug-bash/2020-12/msg00066.html

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-20 Thread Greg Wooledge
On Wed, Mar 20, 2024 at 07:11:34AM -0400, Zachary Santer wrote: > On Wed, Mar 20, 2024 at 12:29 AM Lawrence Velázquez wrote: > > A couple of previous discussions: > > - https://lists.gnu.org/archive/html/bug-bash/2020-12/msg00066.html > > - https://lists.gnu.org/archive/html/bug-bash/2023-06/m

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-20 Thread Zachary Santer
On Wed, Mar 20, 2024 at 12:29 AM Lawrence Velázquez wrote: > > This isn't specific to ${var[@]@k}. > > $ kv1='a 1 b 2 c 3' > $ kv2=(a 1 b 2 c 3) > $ declare -A aa1=($kv1) aa2=(${kv2[@]}) aa3=("${kv2[@]}") > $ declare -p aa1 aa2 aa3 > declare -A aa1=(["a 1 b

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-19 Thread Lawrence Velázquez
On Tue, Mar 19, 2024, at 11:18 PM, Zachary Santer wrote: > As such, I would expect that > $ declare -A assoc_2=( "${assoc_1[@]@k}" ) > would create assoc_2 as a duplicate of assoc_1. However, we see that > the entire expansion becomes the key for a single array element, with > its value being the e

Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-19 Thread Zachary Santer
On Tue, Mar 19, 2024 at 11:18 PM Zachary Santer wrote: > > Repeat-By: > > $ declare -A assoc_1=( [key 0]='value 0' [key 1]='value 1' [key > 2]='value 2' [key 3]='value 3' ) > $ unset assoc_2 > $ declare -A assoc_2 > $ printf '|%s|\n' "${assoc_1[*]@k}" > |key 2 value 2 key 3 value 3 key 0 value 0 k

"${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax

2024-03-19 Thread Zachary Santer
Configuration Information [Automatically generated, do not change]: Machine: x86_64 OS: msys Compiler: gcc Compilation CFLAGS: -march=nocona -msahf -mtune=generic -O2 -pipe -D_STATIC_BUILD uname output: MINGW64_NT-10.0-19045 Zack2021HPPavilion 3.4.10.x86_64 2024-02-10 08:39 UTC x86_64 Msys Machine