Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional associative arrays in Bash

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.

like image 772
Max Avatar asked May 27 '11 08:05

Max


People also ask

Does bash support multidimensional arrays?

Still, multidimensional arrays aren't supported by bash, and we can't get array components that are also arrays. Fortunately, multidimensional arrays can be simulated. This article will provide some illustrations of the simulation of an array of arrays in a bash script.

Does bash have associative arrays?

Bash, however, includes the ability to create associative arrays, and it treats these arrays the same as any other array. An associative array lets you create lists of key and value pairs, instead of just numbered values.

How do you declare associative array in bash?

An associative array can be declared in bash by using the declare keyword and the array elements can be initialized at the time of array declaration or after declaring the array variable. The following script will create an associative array named assArray1 and the four array values are initialized individually.

What is multidimensional associative array?

PHP Multidimensional array is used to store an array in contrast to constant values. Associative array stores the data in the form of key and value pairs where the key can be an integer or string. Multidimensional associative array is often used to store data in group relation.


1 Answers

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" )' 
like image 110
glenn jackman Avatar answered Sep 20 '22 23:09

glenn jackman