![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD. |
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-26-2008 03:19 AM |
| create array holding characters from sring then echo array. | rorey_breaker | Shell Programming and Scripting | 5 | 09-28-2007 08:42 AM |
| Array in awk | koti_rama | Shell Programming and Scripting | 2 | 08-02-2007 08:54 AM |
| looping a array inside inside ssh is not working, pls help | reldb | Shell Programming and Scripting | 5 | 07-07-2006 10:32 AM |
| array | rajan_ka1 | Shell Programming and Scripting | 2 | 03-08-2006 03:03 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||||
|
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
$
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| bash, bash eval, eval |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|