0

Hello I'm using bash and I'm getting stuck iterating on associative bidimensional arrays.

I have this arrays:

declare -A x_matrix

x_matrix[ele1,sub1]="asdfadf"
x_matrix[ele2,sub3]="blabla"
x_matrix[ele1,sub2]="no matters"
x_matrix[ele3,sub1]="opps"

elements=(ele1 ele2 ele3)

And I want to iterate with the known elements in something like this

for e in ${elements[@]}; do
    for sub in ${!x_matrix[$e,@]}; do
        echo "($e,$sub)> ${x_matrix[$e,$sub]}"
    done
done

And I something like:

(ele1,sub1)> asdfadf
(ele1,sub2)> no matters
(ele2,sub3)> blabla
(ele3,sub1)> opps

I can't see how to do this but I believe that it should be possible.

[Edit] I have checked this: BASH: need some help with multidimensional associative arrays And the solution provided didn't fit my needs.

gtunon
  • 43
  • 9
  • 2
    Quoting the man page: "Bash provides one-dimensional indexed and associative array variables.". Do you have the possibility to replace the two dimensional array with 3 different arrays for ele1, ele2 and ele3? – Micha Wiedenmann Jan 29 '18 at 09:03
  • 3
    are you sure you want to use bash for this purpose? there are other scripting languages/programming languages that would be waaaaayyyyyyy more suitable for this kind of data structures – Allan Jan 29 '18 at 09:14
  • 2
    Bash is a shell. Shells have programming language features but there are limitations - you are attempting to do things that shells were not designed for. You are not using the right tool for the job. Consider, for example, python. – cdarke Jan 29 '18 at 10:00
  • Possible duplicate of [BASH: need some help with multidimensional associative arrays](https://stackoverflow.com/questions/6149679/bash-need-some-help-with-multidimensional-associative-arrays) – cdarke Jan 29 '18 at 10:05

2 Answers2

1

You need to deal with it by yourself. For example (works only for your sample data):

[STEP 101] # cat foo.sh
declare -A x_matrix

x_matrix[ele1,sub1]="asdfadf"
x_matrix[ele2,sub3]="blabla"
x_matrix[ele1,sub2]="no matters"
x_matrix[ele3,sub1]="opps"

elements=(ele1 ele2 ele3)

sorted_keys=$( echo ${!x_matrix[@]} | tr ' ' '\n' | sort )

for ele in ${elements[@]}; do
    for k in ${sorted_keys[@]}; do
        if [[ $k == "$ele",* ]]; then
            echo "($k)> ${x_matrix[$k]}"
        fi
    done
done
[STEP 102] # bash foo.sh
(ele1,sub1)> asdfadf
(ele1,sub2)> no matters
(ele2,sub3)> blabla
(ele3,sub1)> opps
[STEP 103] #
pynexj
  • 19,215
  • 5
  • 38
  • 56
1

I don't expect you to accept this, since it does not answer your question. However, as an illustration, this is one way to do this in python. In python, associative arrays are called "dictionaries" and indexed arrays are called "lists".

#!/usr/bin/python
# Create an empty dictionary
x_matrix = {}

# Populate each key with a value of an empty dictionary
x_matrix['ele1'] = {}
x_matrix['ele2'] = {}
x_matrix['ele3'] = {}

# Add values to each dictionary
x_matrix['ele1']['sub1'] = "asdfadf"
x_matrix['ele2']['sub3'] = "blabla"
x_matrix['ele1']['sub2'] = "no matters"
x_matrix['ele3']['sub1'] = "opps"

# Create a list
elements = ['ele1', 'ele2', 'ele3']

# Iterate
for e in elements:
    for sub in x_matrix[e].keys():
        print("(%s,%s)> %s" % (e, sub, x_matrix[e][sub]))

Gives:

(ele1,sub2)> no matters
(ele1,sub1)> asdfadf
(ele2,sub3)> blabla
(ele3,sub1)> opps

Pythonistas will find shorter ways to do it than that, but I want to show the simple steps and to be relatively version neutral (so no "f" strings).

cdarke
  • 42,728
  • 8
  • 80
  • 84
  • Thank you, I have little to none experience with python but this seems to be the way I should go. – gtunon Jan 30 '18 at 09:49
  • 1
    @GuiomarTuñónHita: python is certainly a good skill to have, regardless. If you have any questions about the code then feel free to ask. – cdarke Jan 30 '18 at 13:57