Nested Loop to Echo Multiple Arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested Loop to Echo Multiple Arrays
# 1  
Old 07-12-2005
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
Code:
#!/bin/ksh

# array of locations (usa, london, australia)
set -A location_arr usa ldn aus

# values in each location (3 values each for test)
set -A usa 34 27 84
set -A ldn 83 49 32
set -A aus 25 36 93

# loop to echo each value of each location
for value in ${location_arr[*]}; do
    i=0
    while [ $i -lt 3 ]; do
        echo "${value}[${i}]->[${value}[${i}]]"
        ((i=i+1))
    done
done

output
Code:
usa[0]->[usa[0]]
usa[1]->[usa[1]]
usa[2]->[usa[2]]
ldn[0]->[ldn[0]]
ldn[1]->[ldn[1]]
ldn[2]->[ldn[2]]
aus[0]->[aus[0]]
aus[1]->[aus[1]]
aus[2]->[aus[2]]

the output that I want
Code:
usa[0]->[34]
usa[1]->[27]
usa[2]->[84]
ldn[0]->[83]
ldn[1]->[49]
ldn[2]->[32]
aus[0]->[25]
aus[1]->[36]
aus[2]->[93]


Last edited by yongho; 07-12-2005 at 11:20 AM..
# 2  
Old 07-12-2005
Change your echo statement to:
eval result=\${${value}[${i}]}
echo "${value}[${i}]->[${result}]"
Login or Register to Ask a Question

Previous Thread | Next Thread

9 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

Nested for loop not ending

Hi All, Need help on below script for g in `cat /home/sid.txt` do for h in `cat /home/dev.txt` do symmaskdb -sid $g -dev $h list assign |grep FA |head -1|awk '{print $2}' > tt1.txt done done cat /home/sid.txt ************** 123 235 456 (5 Replies)
Discussion started by: ranjancom2000
5 Replies

4. Shell Programming and Scripting

Nested if loop

Hi Team, I just want to check whether my nested if loop used is correct or not. if ] if ] export1 else export2 fi else if ] export3 else export4 fi fi Thanks Shiva (5 Replies)
Discussion started by: shivashankar_S
5 Replies

5. UNIX for Dummies Questions & Answers

Nested loop code

Greetings, Would anyone be able to tell me why this nested loop doesn't seem to work in any variation? for i in {1..8} do echo "i is "$i for j in {1..i} do echo "j is "$j done doneoutput is always along the lines of i is 1... (7 Replies)
Discussion started by: barnhillec
7 Replies

6. Shell Programming and Scripting

Nested loop in Unix

Hi, I have the following script which is two while loops, but it is working only for the Inner loop without going back to the outer loop. the aim of this script is to remove data files from memory after each five times for each setting of the rotate parameter #!/bin/csh set hdir =... (1 Reply)
Discussion started by: moon218
1 Replies

7. UNIX for Dummies Questions & Answers

Nested Echo Command Help

I am doing below : $HOST_NAME1="webisstg70" $count=1 $echo $HOST_NAME1 $count webisstg70 1 $HOST_NAME="" $HOST_NAME=`echo '$HOST_NAME'${count}` $echo $HOST_NAME HOST_NAME1 $NODE_NAME=`echo $`echo ${HOST_NAME}`` I get below error message (2 Replies)
Discussion started by: findprakash
2 Replies

8. Shell Programming and Scripting

nested loop

I have two do loops. When I break of the inner loop it doesn't go back to the outer loop but exit the program. (5 Replies)
Discussion started by: chinog
5 Replies

9. Shell Programming and Scripting

Nested Arrays

Hi, I'm trying to implement nested arrays in ksh. i've the follwing arrays SRV=\ "SRV1 "\ "SRV2 " SRV1=\ "MD11 "\ "MD12 " SRV2=\ "MD21 "\ "MD22 " MD11=\ "ABC " (5 Replies)
Discussion started by: guysporty
5 Replies
Login or Register to Ask a Question