bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Changing the way bash expands associative array subscripts


From: Koichi Murase
Subject: Re: Changing the way bash expands associative array subscripts
Date: Tue, 13 Apr 2021 18:18:22 +0900

2021年4月13日(火) 18:01 Koichi Murase <myoga.murase@gmail.com>:
> 2021年4月13日(火) 0:16 Chet Ramey <chet.ramey@case.edu>:
> > On 4/6/21 12:46 PM, Koichi Murase wrote:
> > > Looking at another thread
> > > https://lists.gnu.org/archive/html/bug-bash/2021-04/threads.html#00051,
> > > I'm now also interested in how we can handle the indirect expansions
> > > for 'a[@]' and the namerefs for 'a[@]':

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.

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? 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=@ (*)
  iref=indexed[@]; ${!iref} # all the elements
  iref=assoc[@]; ${!iref} # the element of key=@ (*)
  declare -n nref=indexed[@] # reference all the elements
  declare -n nref=assoc[@] # reference the element of key=@ (*)
  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 '@' (*)
  printf -v 'indexed[@]' xxx # error
  printf -v 'assoc[@]' xxx # assign to the element of key=@ (*)

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
  ${indexed[@]}, ${assoc[@]} # all the values
  (( indexed[@] )), (( assoc[@] )) # all the values (error with
multiple non-empty values)

--
Koichi



reply via email to

[Prev in Thread] Current Thread [Next in Thread]