![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
| Forum Sponsor | ||
|
|
|
||||
|
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
$
|
|
||||
|
Quote:
hey i got alternative method as follows, while [ $j -le $no_loop_thru ] ; do var1=$srch_fld[$j] echo ${!var1} ((j=j+1)) done |
|
||||
|
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 ????? |
|
||||
|
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}" |
|
|||
|
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
$
|
|||
| Google UNIX.COM |