bash syntax for accessing an associative array with a value from a different associative array

41 views Asked by At

Good morning,

I have here:

declare A- accocArray1=(["a01"]=1 ["a02"]=2 ["a03"]=3)
declare A- accocArray2=(["b01"]=1 ["b02"]=2 ["b03"]=3)
echo ${accocArray2["${accocArray1["a01"]}"]} ${accocArray2["${accocArray1["a02"]}"]} ${accocArray2["${accocArray1["a03"]}"]}

output:

1 2 3

I am having issues getting the syntax right in the "echo" statement. Any solution would be greatly appreciated.

1

There are 1 answers

0
choroba On
  1. Use -A, not A-;
  2. You don't use the values of the other array, you use the keys. Use the actual values;
  3. BTW, quoting the keys is not needed (but doesn't hurt).
#!/bin/bash
declare -A accocArray1=([a01]=b01 [a02]=b02 [a03]=b03)
declare -A accocArray2=([b01]=1 [b02]=2 [b03]=3)
echo "${accocArray2["${accocArray1[a01]}"]}" "${accocArray2["${accocArray1[a02]}"]}" "${accocArray2["${accocArray1[a03]}"]}"