35

I'm trying to create a multidimensional associative array but need some help. I have reviewed the page suggested in this SO answer but it confused me even more. So far here is what I have:

The script:

#!/bin/bash
declare -A PERSONS
declare -A PERSON
PERSON["FNAME"]='John'
PERSON["LNAME"]='Andrew'
PERSONS["1"]=${PERSON[@]}
PERSON["FNAME"]='Elen'
PERSON["LNAME"]='Murray'
PERSONS["2"]=${PERSON[@]}
for KEY in "${!PERSONS[@]}"; do
 TMP="${PERSONS["$KEY"]}"
 echo "$KEY - $TMP"
 echo "${TMP["FNAME"]}"
 echo "${TMP["LNAME"]}"
done

The output:

1 - John Andrew
John Andrew
John Andrew
2 - Elen Murray
Elen Murray
Elen Murray

As you can see trying to access a specific index of the $TMP array in the for loop returns the whole array.

[Q] What do I need to do in order to separately access the "FNAME" and "LNAME" indexes of the $TMP array inside the for loop?

Thanks.

Community
  • 1
  • 1
Max
  • 12,794
  • 30
  • 90
  • 142
  • See also: https://stackoverflow.com/questions/25221078/nested-associative-arrays-in-bash and https://unix.stackexchange.com/questions/323790/combining-3-separate-arrays-to-one-multidimensional-array-in-bash. – Gabriel Staples Jan 30 '21 at 05:37

2 Answers2

49

You can't do what you're trying to do: bash arrays are one-dimensional

$ declare -A PERSONS
$ declare -A PERSON
$ PERSON["FNAME"]='John'
$ PERSON["LNAME"]='Andrew'
$ declare -p PERSON
declare -A PERSON='([FNAME]="John" [LNAME]="Andrew" )'
$ PERSONS[1]=([FNAME]="John" [LNAME]="Andrew" )
bash: PERSONS[1]: cannot assign list to array member

You can fake multidimensionality by composing a suitable array index string:

declare -A PERSONS
declare -A PERSON

PERSON["FNAME"]='John'
PERSON["LNAME"]='Andrew'
i=1
for key in "${!PERSON[@]}"; do
  PERSONS[$i,$key]=${PERSON[$key]}
done

PERSON["FNAME"]='Elen'
PERSON["LNAME"]='Murray'
((i++))
for key in "${!PERSON[@]}"; do
  PERSONS[$i,$key]=${PERSON[$key]}
done

declare -p PERSONS
# ==> declare -A PERSONS='([1,LNAME]="Andrew" [2,FNAME]="Elen" [1,FNAME]="John" [2,LNAME]="Murray" )'
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • I tried your solution but that's not what I'm looking for as when I loop through $PERSONS I get these 4 elements in the following order: "1,LNAME = Andrew", "2,FNAME = Elen", "1,FNAME = John", and "2,LNAME - Murray" with which I can't work. I'm still puzzled as to why bash cannot support multidimensional arrays. At the end of the day all I'm doing is assigning a variable (the $PERSON array) to an array element ($PERSONS["X"]). Why would it matter what the type of this variable i? Does bash check and prevent assigning an array to an array element? – Max May 27 '11 at 12:07
  • 3
    @user359650, it's a design decision of the bash developers. Quoting from the bash manual I lined to: "Bash provides one-dimensional indexed and associative array variables." What you're doing is assigning a string ("John Andrew") to an array index. I think you need to look again at what you're doing -- if you absolutely must have multidimensional arrays, you're using the wrong tool for the job. – glenn jackman May 27 '11 at 13:10
  • 1
    Also, associative arrays have no particular ordering. – glenn jackman May 28 '11 at 00:01
12

I understand what you need. I also wanted the same for weeks. I was confused whether to use Python or Bash. Finally, exploring something else I found this Bash: How to assign an associative array to another variable name (e.g. rename the variable)?

Here, I got to know how to assign some string and use it later as command. Then with my creativity I found solution to your problem as below:-


#!/bin/bash

declare -A PERSONS
declare -A PERSON

PERSON["FNAME"]='John'
PERSON["LNAME"]='Andrew'
string=$(declare -p PERSON)
#printf "${string}\n"
PERSONS["1"]=${string}
#echo ${PERSONS["1"]}

PERSON["FNAME"]='Elen'
PERSON["LNAME"]='Murray'
string=$(declare -p PERSON)
#printf "${string}\n"
PERSONS["2"]=${string}
#echo ${PERSONS["2"]}

for KEY in "${!PERSONS[@]}"; do
   printf "$KEY - ${PERSONS["$KEY"]}\n"
   eval "${PERSONS["$KEY"]}"
   printf "${PERSONS["$KEY"]}\n"
   for KEY in "${!PERSON[@]}"; do
      printf "INSIDE $KEY - ${PERSON["$KEY"]}\n"
   done
done

OUTPUT:-

1 - declare -A PERSON='([FNAME]="John" [LNAME]="Andrew" )'

declare -A PERSON='([FNAME]="John" [LNAME]="Andrew" )'

INSIDE FNAME - John

INSIDE LNAME - Andrew

2 - declare -A PERSON='([FNAME]="Elen" [LNAME]="Murray" )'

declare -A PERSON='([FNAME]="Elen" [LNAME]="Murray" )'

INSIDE FNAME - Elen

INSIDE LNAME - Murray


The problem actually with multi dimensional arrays in bash and specifically in your approach is that you are assigning PERSON array values to the array element PERSONS[1] which is converted to a list and not an assoc array when you assigned it. And so it no longer will take it as 2 elements of an array as you are not keeping any info about the array data structure in your value. So, I found this hack to be sufficient with only 1 limitation that you will have to do this each time you want to do store/retrieve values. But it shall solve your purpose.

sameer Soni
  • 121
  • 1
  • 2