Iterating through two arrays in a single loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Iterating through two arrays in a single loop
# 1  
Old 07-19-2011
Iterating through two arrays in a single loop

Hey everyone.
Is it possible to use two arrays in a loop?
Basically what I am trying to do is iterate through the elements in an array, and, based on a condition, use the current position in the array to point to that index in the array. Here's the loop structure I'm looking for:

geoDirs=("/logsync/GEO/DBS" "/logsync/GEO/NS")
localDirs=("/logsync/DBS" "/logsync/NS")

Code:
for i in "${geoDirs[@]}"
        do
                if [ "$(ls -A $i)" ]
                        then
                                echo "Take action on $i"
                                echo "Also take action on ${localDirs[@]}"
                fi
        done

I know that code doesn't work, I'm just trying to figure out how to make use of the loop to iterate through both geoDirs and localDirs.

---------- Post updated at 03:07 PM ---------- Previous update was at 03:02 PM ----------

I have a temporary workaround for now, but is there a more elegant solution than:

Code:
geoDirs=("/logsync/GEO/DBS" "/logsync/GEO/NS")
localDirs=("/logsync/DBS" "/logsync/NS")
counter=0

for i in "${geoDirs[@]}"
        do
                if [ "$(ls -A $i)" ]
                        then
                                echo "Take action on $i"
                                echo "Also take action on ${localDirs[$counter]}"
                fi
        counter=$((counter+1))
        done

# 2  
Old 07-19-2011
You can loop through indeces of array like this:
Code:
for i in `seq 0 ${#geoDirs[@]}` ;  do
   echo $i  #i=0,1,2,3...
   #process ${geoDirs[$i]}
   #process ${localDirs[$i]}
done

${#array[@]} return number of elements of array
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple arrays in variable using for loop

Hi, I'm trying to get the number of files inside same kind of folders on each disks and assigning each values in to a variable named with same folder and disk name so that it'll be easy for me to identify each time.But somehow I'm not able to assign those values in that specific name variable... (1 Reply)
Discussion started by: ratheeshp
1 Replies

2. Shell Programming and Scripting

Loop over multiple arrays

Hi All I need really really help with this :- I have two files ( File1 , File 2) both files are output of two different scripts. File1 usually has a list of names ( sometimes 3 names sometimes 5 sometimes more , depends about the output of the script) File2 usually has a list of numbers... (2 Replies)
Discussion started by: samsan
2 Replies

3. Shell Programming and Scripting

Bash for loop with arrays second variable?

I am fairly new to bash and am not sure how to resolve this: I have a series of geographical long/lat points eg. 50/-30 listed on separate lines in a file called junk2. I have input these into an array and am then using that array in a for loop. Towards the end of the loop I create a file called... (4 Replies)
Discussion started by: lily-anne
4 Replies

4. Shell Programming and Scripting

Problem with arrays and loop

Hello , im sorry for my english . im trying to create a dynamic menu that will display if the interface is ACTIVE OR STOPPED/FAILED for some reason i cant get it to work properly start_interface_func() { i=0 for interface_chk in 11 71 73 72 12 47 48 49 50 20 23 24 25 46 21 22 27 28... (5 Replies)
Discussion started by: visiown
5 Replies

5. Programming

Perl arrays and loop through array

Hi All I need to get <STDIN> from a user. the <STDIN> is a range of number delimited by "," (comma) and can be with range delimited by "-". Example: 1,2,3,4-9,12,15,34-36,70 Now I need to get this from the user and go on each number and "Do something"... but when trying to do this as above... (2 Replies)
Discussion started by: RedGrinGo
2 Replies

6. Shell Programming and Scripting

Problem iterating in while loop

I am facing a problem in replacing the file contents by iterating through the list. My present code: Code: #!/bin/bash# TFILE="/tmp/vinay/testb_1.txt" while read linedo aline="$line" echo $aline code=`echo $aline|cut -d ',' -f1` country=`echo $aline|cut -d... (5 Replies)
Discussion started by: av_vinay
5 Replies

7. Shell Programming and Scripting

How to access the elements of two arrays with a single loop using the inbuilt index.

Hi all, I wanted to access two arrays (of same size) using one for loop. Ex: #!/bin/bash declare -a num declare -a words num=(1 2 3 4 5 6 7) words=(one two three four five six seven) for num in ${num} do echo ":$num: :${words}:" done Required Output: :1: :one: (11 Replies)
Discussion started by: 14341
11 Replies

8. Shell Programming and Scripting

Using arrays outside the loop

set -A town_name india pakistan srilanka india set -A town m=0 n=0 while } ] do t1=`echo ${town_name}` town= $t1 echo ${town} n=$((n+1)) m=$((m+1)) done t2=`echo ${town}` echo $t2 i m trying to get the value of town array outside the loop but i m nt getting it.. Could u plz... (5 Replies)
Discussion started by: priyanka3006
5 Replies

9. Shell Programming and Scripting

Nested Loop to Echo Multiple Arrays

I have three arrays which hold three elements each. I have a fourth array which contains the names of those three arrays. I'm having difficulty creating a nested loop that can loop through each array and echo their values. script #!/bin/ksh # array of locations (usa, london, australia)... (1 Reply)
Discussion started by: yongho
1 Replies

10. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies
Login or Register to Ask a Question