[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Changing the way bash expands associative array subscripts
From: |
Chet Ramey |
Subject: |
Re: Changing the way bash expands associative array subscripts |
Date: |
Sun, 18 Apr 2021 17:46:08 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.9.1 |
On 4/13/21 5:18 AM, Koichi Murase wrote:
I actually doubt whether there are real use cases for the behavior
that «iref=a[@]; ${!iref}» or «declare -n nref=a[@]; $nref» expands to
all the elements of *associative* arrays. Maybe it is useful to
expand the indexed arrays in this way because indexed arrays contain a
sequence of values. But associative arrays usually contain a set of
unordered key-value pairs, so I guess there are not so many chances to
expand only the values without specifying keys.
I'd dispute that. There are multiple use cases for the set of values in an
associative array.
It's just a naive idea and may be imperfect, but how about changing
the behavior of assoc[@] (except for "${assoc[@]}") only for
associative arrays while keeping the current behavior for indexed
arrays?
Some of this is reasonable.
In the following list, the items marked with (*) are the
behavior different from the current one. I listed examples for key=@,
but the same for key=*.
unset 'indexed' # remove the indexed array
unset 'assoc' # remove the associative array
unset 'indexed[@]' # empty the array (*)
unset 'assoc[@]' # remove the element of key=@ (*)
We've already discussed these. I'm ok with the change to associative
arrays, since there's already a way to do it and it seems more
straightforward to remove the element with key `@'. I'd have to think about
the changed to indexed arrays, since you can already do it with
`indexed=()'. It's a novel new use case.
iref=indexed[@]; ${!iref} # all the elements
iref=assoc[@]; ${!iref} # the element of key=@ (*)
No, there's no reason to have ${!iref} and ${assoc[@]} behave differently.
declare -n nref=indexed[@] # reference all the elements
declare -n nref=assoc[@] # reference the element of key=@ (*)
Same reason.
test -v 'indexed[@]'
[ -v 'indexed[@]' ]
[[ -v indexed[@] ]] # test if the indexed array is non-empty
test -v 'assoc[@]'
[ -v 'assoc[@]' ]
[[ -v assoc[@] ]] # test if the associative array has the key '@' (*)
I'm ok with this.
printf -v 'indexed[@]' xxx # error
printf -v 'assoc[@]' xxx # assign to the element of key=@ (*)
I think this is ok, too, for the same reason as:
We don't change the following behavior of associative arrays where the
syntactic information may distinguish a[$key] from a[@]:
indexed[@]=xxx, assoc[@]=xxx # error
I think it's ok to allow
assoc[@]=value
since
indexed[@]=xxx is going to be an error no matter which behavior you choose
because it's an invalid arithmetic expression.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/
- Re: Changing the way bash expands associative array subscripts, (continued)
- Re: Changing the way bash expands associative array subscripts, Koichi Murase, 2021/04/06
- Re: Changing the way bash expands associative array subscripts, Greg Wooledge, 2021/04/06
- Re: Changing the way bash expands associative array subscripts, Chet Ramey, 2021/04/08
- Re: Changing the way bash expands associative array subscripts, Greg Wooledge, 2021/04/08
- Re: Changing the way bash expands associative array subscripts, Koichi Murase, 2021/04/08
- Re: Changing the way bash expands associative array subscripts, Chet Ramey, 2021/04/09
- Re: Changing the way bash expands associative array subscripts, Koichi Murase, 2021/04/09
- Re: Changing the way bash expands associative array subscripts, Chet Ramey, 2021/04/12
- Re: Changing the way bash expands associative array subscripts, Koichi Murase, 2021/04/13
- Re: Changing the way bash expands associative array subscripts, Koichi Murase, 2021/04/13
- Re: Changing the way bash expands associative array subscripts,
Chet Ramey <=
- Re: Changing the way bash expands associative array subscripts, Koichi Murase, 2021/04/19
- Re: Changing the way bash expands associative array subscripts, Chet Ramey, 2021/04/13
- Re: Changing the way bash expands associative array subscripts, Koichi Murase, 2021/04/13
- Re: Changing the way bash expands associative array subscripts, Koichi Murase, 2021/04/13
- Re: Changing the way bash expands associative array subscripts, Chet Ramey, 2021/04/15
- Re: Changing the way bash expands associative array subscripts, konsolebox, 2021/04/15
- Re: Changing the way bash expands associative array subscripts, Koichi Murase, 2021/04/16
- Re: Changing the way bash expands associative array subscripts, Chet Ramey, 2021/04/18
- Re: Changing the way bash expands associative array subscripts, konsolebox, 2021/04/18
- Re: Changing the way bash expands associative array subscripts, Koichi Murase, 2021/04/19