In the following code, I create some associative arrays in a loop. The consist of two strings, a string identifier and a year. After creation, I want to access the arrays in a loop based on the identifier only.
#!/bin/bash
# Declare associative arrays of journal-year combinations
A_JOURNAL_LIST={JF,JFE,RFS}
B_JOURNAL_LIST={JBF,JFI,JMCB}
ALL_JOURNAL_LIST={JF,JFE,RFS,JBF,JFI,JMCB}
for year in {1998..2000} {2009..2011}
do
eval "A_$year=($A_JOURNAL_LIST-$year) ;"
eval "B_$year=($B_JOURNAL_LIST-$year) ;"
eval "all_$year=($ALL_JOURNAL_LIST-$year) ;"
done
Here I easily get bunch of arrays of the form A_1999 which e.g. expands to JF-1999 JFE-1999 RFS-1999 and so on.
for journal in A B all
do
echo "${'$journal'_1999[@]}"
done
I expect
JF-1999 JFE-1999 RFS-1999
JBF-1999 JFI-1999 JMCB-1999
JF-1999 JFE-1999 RFS-1999 JBF-1999 JFI-1999 JMCB-1999
I got a bad substitution error all the time and I tried a lot of combinations. What's wrong?