Array inside an array


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Array inside an array
# 1  
Old 08-22-2007
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
# 2  
Old 08-22-2007
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

$

# 3  
Old 08-23-2007
Quote:
Originally Posted by Perderabo
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
# 4  
Old 08-23-2007
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 ?????
# 5  
Old 08-23-2007
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}"
# 6  
Old 06-10-2008
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 ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Array initialization inside class in C++

const int VALUES = {7,4,2,1,0}; //or int VALUES = {7,4,2,1,0};this statement inside a class definition gives error. Why? (3 Replies)
Discussion started by: milhan
3 Replies

2. UNIX for Dummies Questions & Answers

Array inside sed

Hi guys Let me at first describe the whole thing that I'm trying to do. Lets say I have 100 files like the following one. Ow 1230 16.000000 -0.834000 16.083957 1.751652398 -17.20094528 -4.450623277 Hw 1231 ... (6 Replies)
Discussion started by: saleheen
6 Replies

3. Shell Programming and Scripting

How to use variable inside array?

I tried to use variable inside an array variable, but its not working as expected.:wall: ENV1=123 ENV1=789 ENV1=120 ENV2=567 if then name=ENV1 echo "${name}" echo "${name}" echo "${name}" else name=ENV1 echo "${name}" fi Output: ./val.sh 1 123 (2 Replies)
Discussion started by: Jayavinoth
2 Replies

4. Shell Programming and Scripting

unique inside array

I have a file root@server # cat /root/list12 11.22.33.44 22.33.44.55 33.44.55.66 33.44.55.66 33.44.55.66 I try to pass to array and display unique. root@server# cat /root/test12.sh #!/bin/bash #delcare array badips and accumulate values to array elemenrs badips=( $( cat... (4 Replies)
Discussion started by: anil510
4 Replies

5. Shell Programming and Scripting

Calling array inside awk

Hello I have the file df.tmp FS is actually the / FS but escape character\ and end of line $ is used in order to fetch exctly / and not other filesystems. awk '/\/$/ {print $(NF-1)+0}' df.tmp will work properly and return a value eg. 60 but when I am trying to issue the command with the array... (3 Replies)
Discussion started by: drbiloukos
3 Replies

6. Shell Programming and Scripting

printing array elements inside AWK

i just want to dump my array and see if it contains the values i am expecting. It should print as follows, ignore=345fht ignore=rthfg56 . . . ignore=49568g Here is the code. Is this even possible to do? please help termReport.pl < $4 | dos2ux | head -2000 | awk ' BEGIN... (0 Replies)
Discussion started by: usustarr
0 Replies

7. Shell Programming and Scripting

How to print array values whose name is inside a variable

I have a array as CArray=( a1 a2 ) and a1,a2,a3 are also array as: a1=(1 2 3) a2=(3 4 5) now I have this in my code: for i in `echo "${CArray}"` do echo ${$i} done It is giving error as :"bad substitution" It should give me value as 1 2 3 3 4 5 how can I get this...Can u please... (2 Replies)
Discussion started by: joshilalit2004
2 Replies

8. Shell Programming and Scripting

using array inside awk

Hi All, I have the following code sequence for reading some bulk file and moving the content to two different arrays. while read data do THREEG_PATTERN=`echo $data | egrep "3G"` if then NEW_THREEG_PATTERN=`echo $THREEG_PATTERN | cut -d " " -f2` ... (12 Replies)
Discussion started by: subin_bala
12 Replies

9. Shell Programming and Scripting

split and making an array inside another array

I want to run an awk split on a value that has been pushed through an array and I was wondering what the syntax should be?? e.g. running time strings through an array and trying to examine just minutes: 12:25:30 10:15:13 08:55:23 awk ' NR==FNR{ ... (2 Replies)
Discussion started by: dcfargo
2 Replies

10. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies
Login or Register to Ask a Question