[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: "${assoc[@]@k}" doesn't get expanded to separate words within compou
From: |
Lawrence Velázquez |
Subject: |
Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax |
Date: |
Sun, 24 Mar 2024 16:07:40 -0400 |
User-agent: |
Cyrus-JMAP/3.11.0-alpha0-332-gdeb4194079-fm-20240319.002-gdeb41940 |
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 <chet.ramey@case.edu> 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 you do with @k?
>
> You can write the keys and values to a stream/file.
I guess you can also more easily iterate over them or move them to
an indexed array (as has already been demonstrated). I'm not really
sure this merited its own parameter transformation, but then again
that's how I feel about most of the transformations.
>> The difference in expansion behavior between indexed and associative
>> array compound assignment statements doesn't make sense.
The behavior of the latter is for backward compatibility, I think.
Initially expansions could only be used in associative array compound
assignments like this, so word splitting was undesirable:
declare -A x=([$y]=$z)
I suppose the contents of x=(...) could be examined lexically to
determine whether splitting should be allowed, but that would make
the situation even messier.
> 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 hash=(...)
> kvlist=( "${hash[@]@k}" )
>
> declare -A newhash
> addtoarray newhash "${kvlist[@]}"
>
> Some readers may observe that this looks a bit like Tcl's "array set"
> command. That's not a coincidence. Whenever I see "a list of alternating
> keys and values", that's where my mind goes.
>
> I'm debating mentally whether this hypothetical new builtin would only
> work with associative arrays, or also double as an "lappend" (append new
> elements to the end of a list/array) if given an indexed array name.
Incidentally, yash has an "array" builtin that works like this on
indexed arrays. (Yash doesn't have associative arrays.)
https://magicant.github.io/yash/doc/_array.html
--
vq
- Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax, (continued)
- Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax, Zachary Santer, 2024/03/24
- Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax, Oğuz, 2024/03/24
- Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax, Greg Wooledge, 2024/03/24
- Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax, Oğuz, 2024/03/24
- Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax, Oğuz, 2024/03/24
- Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax, Greg Wooledge, 2024/03/24
- Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax,
Lawrence Velázquez <=
- Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax, Dennis Williamson, 2024/03/24
- Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax, Greg Wooledge, 2024/03/24
- Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax, Dennis Williamson, 2024/03/24
- Re: "${assoc[@]@k}" doesn't get expanded to separate words within compound assignment syntax, Chet Ramey, 2024/03/25