Skip to main content
added 1173 characters in body
Source Link
glenn jackman
  • 88.5k
  • 16
  • 124
  • 179

variable indirection will be helpful here:

for journal in A B all
do
    indirect="${journal}_1999[@]"
    echo "$journal: ${!indirect}"
done

outputs

A: JF-1999 JFE-1999 RFS-1999
B: JBF-1999 JFI-1999 JMCB-1999
all: JF-1999 JFE-1999 RFS-1999 JBF-1999 JFI-1999 JMCB-1999

An eval-free rewrite. Arrays of arrays is not something bash is natively suited for, so I have to use space-separated strings and temp storage

# Declare associative arrays of journal-year combinations
a_journal_list=( {JF,JFE,RFS} )
b_journal_list=( {JBF,JFI,JMCB} )
all_journal_list=( "${a_journal_list[@]}" "${b_journal_list[@]}" )
declare -a a b all

for year in {1998..2000} {2009..2011}
do
    # store year-specific values as space-separated strings
    a[$year]=$( printf "%s-$year " "${a_journal_list[@]}" )
    b[$year]=$( printf "%s-$year " "${b_journal_list[@]}" )
    all[$year]=$( printf "%s-$year " "${all_journal_list[@]}" )
done  

selected_years=( 1998 1999 2000 )
for journal in a b all
do
    # I'll use the positional params for temp storage of the accumulated array
    set --
    for year in "${selected_years[@]}"
    do
        indirect="${journal}[$year]"
        # variable is unquoted to allow word splitting
        set -- "$@" ${!indirect}
    done
    echo $journal
    printf "%s\n" "$@"
done

variable indirection will be helpful here:

for journal in A B all
do
    indirect="${journal}_1999[@]"
    echo "$journal: ${!indirect}"
done

outputs

A: JF-1999 JFE-1999 RFS-1999
B: JBF-1999 JFI-1999 JMCB-1999
all: JF-1999 JFE-1999 RFS-1999 JBF-1999 JFI-1999 JMCB-1999

variable indirection will be helpful here:

for journal in A B all
do
    indirect="${journal}_1999[@]"
    echo "$journal: ${!indirect}"
done

outputs

A: JF-1999 JFE-1999 RFS-1999
B: JBF-1999 JFI-1999 JMCB-1999
all: JF-1999 JFE-1999 RFS-1999 JBF-1999 JFI-1999 JMCB-1999

An eval-free rewrite. Arrays of arrays is not something bash is natively suited for, so I have to use space-separated strings and temp storage

# Declare associative arrays of journal-year combinations
a_journal_list=( {JF,JFE,RFS} )
b_journal_list=( {JBF,JFI,JMCB} )
all_journal_list=( "${a_journal_list[@]}" "${b_journal_list[@]}" )
declare -a a b all

for year in {1998..2000} {2009..2011}
do
    # store year-specific values as space-separated strings
    a[$year]=$( printf "%s-$year " "${a_journal_list[@]}" )
    b[$year]=$( printf "%s-$year " "${b_journal_list[@]}" )
    all[$year]=$( printf "%s-$year " "${all_journal_list[@]}" )
done  

selected_years=( 1998 1999 2000 )
for journal in a b all
do
    # I'll use the positional params for temp storage of the accumulated array
    set --
    for year in "${selected_years[@]}"
    do
        indirect="${journal}[$year]"
        # variable is unquoted to allow word splitting
        set -- "$@" ${!indirect}
    done
    echo $journal
    printf "%s\n" "$@"
done
Source Link
glenn jackman
  • 88.5k
  • 16
  • 124
  • 179

variable indirection will be helpful here:

for journal in A B all
do
    indirect="${journal}_1999[@]"
    echo "$journal: ${!indirect}"
done

outputs

A: JF-1999 JFE-1999 RFS-1999
B: JBF-1999 JFI-1999 JMCB-1999
all: JF-1999 JFE-1999 RFS-1999 JBF-1999 JFI-1999 JMCB-1999