Nested Arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested Arrays
# 1  
Old 04-11-2005
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 "

MD12=\
"EFG "

MD21=\
"XYZ "

MD22=\
"PQR "

for i in $SRV
do
echo $i

for j in $i
do
echo $j
done
done


The output of this is
SRV1
SRV1

What i need is the output should be
SRV1
MD11


What its doing is substituting the value of $i in the for loop where as what is actually required is $$i. however this syntax is not acceptable

Can anyone help me out on this as to how to achieve it.

Thanks.
# 2  
Old 04-11-2005
I don't exactly follow you here. But I think you're looking for something like:
eval echo \$$i
# 3  
Old 04-12-2005
Thanks for replying..

I'm pasting the entire code once again.

SRV=\
"SRV1 "\
"SRV2 "

SRV1=\
"MD11 "\
"MD12 "

SRV2=\
"MD21 "\
"MD22 "

MD11=\
"ABC "

MD12=\
"EFG "

MD21=\
"XYZ "

MD22=\
"PQR "

MD11() {
echo "MD11"
}

echo "starting loop $SRV"
cnt=0
for i in $SRV
do
echo "\n$SRV"
srv=\$$SRV
echo $srv
for j in $srv
do
echo "$j"
$j
done
done

The output is as follows :

starting Loop SRV1
$MD11
$MD11

This is where the problem is the last out put line is $MD11.. here its not printing the values contained in the array MD11 but its treating it as a value.
Also when the the call is made to $j it should call the function MD11, since MD11 is stored at the base location of the array, instead it prints
try.sh: $MD11: not found

Request your help.
# 4  
Old 04-12-2005
Instead of
$j
try:
eval $j
# 5  
Old 04-13-2005
Hi,

replacing $j with eval $j results in the $SRV1
However if i replace srv=\$$i with srv=eval $i and then echo $j or eval $j
in both cases the output is SRV1

Thanks,

P.S.
sorry for the confusion but in previous reply i mistyped the output to be $MD11.. its $SRV1. ( In the comments, references to MD11 should be read as SRV1 )
# 6  
Old 04-13-2005
Your posts are so confusing that I am unable to determine whether or not you still have a problem. For the record I meant the function invocation where you have a line that reads
$j
with nothing else at all on the line should perhaps be:
eval $j
I'm not sure if you tried that. It also might be that you need a double eval:
eval eval $j
but then again who knows? Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nested if else

Hi, i m trying to create script which logic is like below. if ; then x=`cat /tmp/testoutput.log | grep STOP | wc -l` y=`cat /tmp/testoutput.log | grep RUN | wc -l` if ; then echo "process stop" if ; then echo "process running " else echo "file not found" fi ----------------... (2 Replies)
Discussion started by: tapia
2 Replies

2. 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

3. Shell Programming and Scripting

Nested case

Hi there, I have nested case in my script. I am asking user, want to continue? if user press y/Y then my inner case should continue, rather than that my code start from beginning. I would like to continue my inner case until user press n or N. Is any one tell me how can I do? Thanking You,... (2 Replies)
Discussion started by: kasparov
2 Replies

4. Shell Programming and Scripting

Nested if in KSH

Trying to clean up the last little thing in this script which is that the mv always performs no matter whether it is needed or not. It doesn't cause any bugs just prints an unsightly "can't mv source and dest are the same". Obviously I want to get rid of it but having issues getting the... (4 Replies)
Discussion started by: Calbrenar
4 Replies

5. UNIX for Dummies Questions & Answers

Nested If in Unix

Hi!! LookVar=`find . -name "${input}" | wc -w` if then cd $input rm -f * ftp -n -i $HostName << EOF quote USER $User quote PASS $Password cd $Path SoLookVar=`find . -name "${input}" | wc -w` echo $SoLookVar if then cd $input mget ./* bye EOF chmod 775 ./* (12 Replies)
Discussion started by: Afsana
12 Replies

6. Programming

Help nested sql

Hi, I bought a module called "Price comparison listing" for Prestashop that was supposed to help me push product listing to kelkoo. Unfortunately it doesnt work and the developers don't fix, and they don't refund.... I have reported this to prestashop, but I need a solution.. Be careful buying... (1 Reply)
Discussion started by: joje47
1 Replies

7. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

8. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

9. Shell Programming and Scripting

nested variables

Is there any way to do variable nesting using sh? For example: example_1="a test string" example_2="another test" example_3="etc..." i=2 echo ${example_$i} The shell reports: sh: ${example_$i}: bad substitution If not, maybe someone could suggest another method. Thanks in... (3 Replies)
Discussion started by: kevinl33
3 Replies

10. 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
Login or Register to Ask a Question