1

I want to iterate over an array which name is in another variable. This is what I tried so far:

NTASKSLIST=(384 768 1536 3072)

# there are potentially more arrays like these, each custom defined for every entry in NTASKSLIST
OV384LIST=("16 16 12" "16 16 16" "18 18 12" "18 18 16" "18 18 18")
OV768LIST=("24 16 16" "24 18 18" "32 18 18" "36 18 18" "36 24 24")

for NTASKS in "${NTASKSLIST[@]}"
do

  OVLIST="OV${NTASK}LIST"
  # OVLIST now holds the name of the array I want to iterate

  # now I want to iterate over the array which name is in OVLIST but it does not work
  for OV in "${!OVLIST[@]}"
  do
    echo $OV # prints "0" because OVLIST has only 1 item (the name of the array I want to iterate)

    # here I want to do stuff with OV and NTASKS

  done
done

Do you have any suggestions how to do this? Alternative approaches which accomplish the same thing should to as well.

Edit:

As shown the comments by don_crissti the answer to my question is:

    OVNAME="OV${NTASKS}LIST"[@]
    OVLIST=( "${!OVNAME}" )

    for OV in "${OVLIST[@]}"
3
  • so, in your case the answer there translates to (why do you use caps?): for ntask in "${ntaskslist[@]}"; do ovlist="ov${ntask}list"[@]; new=( "${!ovlist}" ); for element in "${new[@]}"; do printf %s\\n "$element";done; done – don_crissti Nov 1 '16 at 10:57
  • I used caps because I thought that this is convention in Unix, but stackoverflow.com/questions/673055/… told me something different. But it shouldn't make any difference, should it? – new2f7 Nov 1 '16 at 11:59
  • Sry, my fault. I updated my post. – new2f7 Nov 2 '16 at 10:37

Browse other questions tagged or ask your own question.