0

I have defined multiple arrays:

array1=(el1 el2 el3 el4 el5)
array2=(el10 el12 el14)
array3=(el5 el4 el11 el8)

I need to iterate through all elements of all arrays. The following is the syntax I use:

for j in {1..3}
do
    for (( k = 0 ; k < ${#array$j[*]} ; k++ ))
    do
        #perform actions on array elements, refer to array elements as "${array$j[$k]}"
    done
done

However, the above snippet fails with the message(s)

k < ${#array$j[*]} : bad substitution and 
${array$j[$k]}: bad substitution

What is wrong with my array syntax?

4
  • Do you really need $j and $k, or can you just write for value in "${array1[@]}" "${array2[@]}" "${array3[@]}" ; do ... ; done? Commented Jul 26, 2013 at 21:00
  • unfortunately, I do. Actual number of arrays is 11, and actions on elements within each array need to be distinct. Commented Jul 26, 2013 at 21:10
  • Your syntax for array assignment is wrong, there shouldn't be any commas. Commented Jul 26, 2013 at 21:10
  • Apologies - that was just a typo I made when writing the question. Array specification in my script doesn't have any commas. Commented Jul 26, 2013 at 21:48

2 Answers 2

1

Your script is not correct.

  1. First of all there should not be comma separating various elements in your 3 arrays.
  2. Then to use array name as variable you need to use indirection

Following should work:

array1=(el1 el2 el3 el4 el5)
array2=(el10 el12 el14)
array3=(el5 el4 el11 el8)

for j in {1..3}
do
    n="array$j[@]"
    arr=("${!n}")
    for (( k = 0 ; k < ${#arr[@]} ; k++ ))
    do
        #perform actions on array elements, refer to array elements as "${array$j[$k]}"
        echo "Processing: ${arr[$k]}"
    done
done

This will process all 3 arrays and gives this output:

Processing: el1
Processing: el2
Processing: el3
Processing: el4
Processing: el5
Processing: el10
Processing: el12
Processing: el14
Processing: el5
Processing: el4
Processing: el11
Processing: el8
Sign up to request clarification or add additional context in comments.

4 Comments

Ha, we just posted the same answer within fifteen seconds of each other. Great minds . . . ! :-P
Wow that was really funny, cant agree more :)
Thank you - that did it. Commas in the array definition was just a typo I made when writing the question. Array specification in my script doesn't have any commas. Use of indirection solved the problem.
If this answer helped you solve your problem, please consider marking it as "accepted", so users facing a similar problem in the future will be able to see it easily.
0

This:

array1=(el1 el2 el3 el4 el5)
array2=(el10 el12 el14)
array3=(el5 el4 el11 el8)

for j in {1..3} ; do
    # copy array$j to a new array called tmp_array:
    tmp_array_name="array$j[@]"
    tmp_array=("${!tmp_array_name}")

    # iterate over the keys of tmp_array:
    for k in "${!tmp_array[@]}" ; do
        echo "\${array$j[$k]} is ${tmp_array[k]}"
    done
done

prints this:

${array1[0]} is el1
${array1[1]} is el2
${array1[2]} is el3
${array1[3]} is el4
${array1[4]} is el5
${array2[0]} is el10
${array2[1]} is el12
${array2[2]} is el14
${array3[0]} is el5
${array3[1]} is el4
${array3[2]} is el11
${array3[3]} is el8

There may well be some way to do this without creating tmp_array, but I couldn't get the indirection to work without it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.