bug-bash
[Top][All Lists]
Advanced

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

Syntax error in a Map/Hash initializer -- why isn't this supported?


From: L A Walsh
Subject: Syntax error in a Map/Hash initializer -- why isn't this supported?
Date: Mon, 10 Aug 2020 14:56:47 -0700

I wanted to use a map that looked like this:

declare -A switches=([num]=(one two three)), where '(one two three)'
is an associated list.  Ideally, I could access it like other arrays:
for types in  ${switches[num][@]}; do...
or
switches[num]=(one two three)    #gives:
                  -bash: switches[num]: cannot assign list to array member
or
echo ${switches[num][0]}  (="one").

I defaulted to going around it by making it a string, like:
switches[num]="one|two|three"
or
switches[num]="(one two three)" but why?  It seems obvious that bash
knows what I'm trying to do, so why not just do it?

Some nested constructs seem to work:
> b=(1 2 3)
> a=(4 5 6)
> echo ${a[${b[1]}]}
6

but more often than not, they don't.  Is there a reason to disallow such?



reply via email to

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