The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
array ccp Shell Programming and Scripting 3 02-25-2008 11:19 PM
create array holding characters from sring then echo array. rorey_breaker Shell Programming and Scripting 5 09-28-2007 05:42 AM
Array in awk koti_rama Shell Programming and Scripting 2 08-02-2007 05:54 AM
looping a array inside inside ssh is not working, pls help reldb Shell Programming and Scripting 5 07-07-2006 07:32 AM
array rajan_ka1 Shell Programming and Scripting 2 03-07-2006 11:03 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-22-2007
manas_ranjan's Avatar
Registered User
 

Join Date: Jul 2007
Location: PUNE
Posts: 157
Array inside an array

hi All,

I have a array as follows,

array1=("xx" "abc" "def" "xyz")
and each array1[index] is also storing some array values, like

array1[1]=abc
and abc=("a" "b" "c") etcetera etcetra.........

Note : each subarray under array1 have index 3 i.e. it can max contain 3 values

if i echo ${abc[index]} [ where index is 1,2,3...] it works fine.

but if i loop thru it's not printing the values, can anybody help me solving this thru loop,

my loop goes like this


cnt=1
total_Array=${#array1[*]}
no_loop_thru=${#abc[*]}

while [ $cnt -lt $total_Array ]
do
srch_fld=`echo ${array1[$cnt]}`
j=1
while [ $j -le $no_loop_thru ]
do
#var1=`$srch_fld[$j]`
#echo ${var1}
echo ${srch_fld[$j]}
j=`expr $j + 1`
done
cnt=`expr $cnt + 1`
done


can anybody suggest any improvement on this......LOOP
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-22-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,355
Code:
$ cat arrayloop
#! /usr/local/bin/bash

abc=(a b c)
def=(d e f)
ghi=(g h i)
array1=(abc def ghi)
cnt=0
total_Array=${#array1[*]}
no_loop_thru=${#abc[*]}

while [ $cnt -lt $total_Array ] ; do
        srch_fld=${array1[$cnt]}
        j=0
        while [ $j -le $no_loop_thru ] ; do
                eval var1=\${$srch_fld[$j]}
                echo ${var1}
                ((j=j+1))
        done
        ((cnt=cnt+1))
done
exit 0
$ ./arrayloop
a
b
c

d
e
f

g
h
i

$
Reply With Quote
  #3 (permalink)  
Old 08-23-2007
manas_ranjan's Avatar
Registered User
 

Join Date: Jul 2007
Location: PUNE
Posts: 157
Quote:
Originally Posted by Perderabo View Post
Code:
$ cat arrayloop
#! /usr/local/bin/bash

abc=(a b c)
def=(d e f)
ghi=(g h i)
array1=(abc def ghi)
cnt=0
total_Array=${#array1[*]}
no_loop_thru=${#abc[*]}

while [ $cnt -lt $total_Array ] ; do
        srch_fld=${array1[$cnt]}
        j=0
        while [ $j -le $no_loop_thru ] ; do
                eval var1=\${$srch_fld[$j]}
                echo ${var1}
                ((j=j+1))
        done
        ((cnt=cnt+1))
done
exit 0
$ ./arrayloop
a
b
c

d
e
f

g
h
i

$

hey i got alternative method as follows,
while [ $j -le $no_loop_thru ] ; do
var1=$srch_fld[$j]
echo ${!var1}
((j=j+1))
done
Reply With Quote
  #4 (permalink)  
Old 08-23-2007
manas_ranjan's Avatar
Registered User
 

Join Date: Jul 2007
Location: PUNE
Posts: 157
The question remains same, but now the representation changed,

let

abc=(a b c)
def=(d e f)
ghi=(g h i)
array1=(abc def ghi)


now thing is in sub array i know the content or size, let here it is 3
so i want my o/p should come like
abc(1)=a
def(1)=d
ghi(1)=g

then
abc(2)=b
def(2)=e
ghi(2)=h


etcetera etcetera.......

can anybody help me out to achieve it ?????
Reply With Quote
  #5 (permalink)  
Old 08-23-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,355
The first element of an array is 0, not 1.
var1=$srch_fld[$j]
echo "${srch_fld}(${j}) = ${!var1}"
will give you the correct indices. If you want to pretend that the first index is 1, you could use:
echo "${srch_fld}($((j+1))) = ${!var1}"
Reply With Quote
  #6 (permalink)  
Old 06-10-2008
Registered User
 

Join Date: Jun 2008
Posts: 1
Modifying the code from Perderabo, how would you handle different-sized arrays in the inner while loop? For example, let's change the code to be:

Code:
$ cat arrayloop
#! /usr/local/bin/bash

abc=(a b c x y)
def=(d e f z)
ghi=(g h i)
array1=(abc def ghi)
cnt=0
total_Array=${#array1[*]}

while [ $cnt -lt $total_Array ] ; do
        srch_fld=${array1[$cnt]}
        j=0
        while [ $j -le <????> ] ; do
                eval var1=\${$srch_fld[$j]}
                echo ${var1}
                ((j=j+1))
        done
        ((cnt=cnt+1))
done
exit 0
$ ./arrayloop
a
b
c
x
y

d
e
f
z

g
h
i

$
We can't use the original no_loop_thru variable since the arrays are of varying length. I realize we need to get the length of srch_fld but I'm unable to get the syntax right ...
Reply With Quote
Google UNIX.COM
Reply

Tags
bash, bash eval, eval

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0