Using arrays outside the loop

set -A town_name india pakistan srilanka india
set -A town
m=0
n=0
while [ $m -lt ${#town_name
[*]} ]
do
t1=`echo ${town_name[$m]}`
town[$n]= $t1
echo ${town[$n]}
n=$((n+1))
m=$((m+1))
done
t2=`echo ${town[3]}`
echo $t2

i m trying to get the value of town array outside the loop but i m nt getting it..
Could u plz help me:confused:

if this is your code -- you're nuking the array in the 2nd line...

set -A town_name india pakistan srilanka india
### set -A town
m=0
n=0
while [ $m -lt ${#town_name[*]} ]
do
t1=`echo ${town_name[$m]}`
town[$n]=$t1
echo ${town[$n]}
n=$((n+1))
m=$((m+1))
done
t2=`echo ${town[3]}`
echo $t2


spacing problems also.

Your problem is the space after the equals sign on the line "town[$n]= $t1"

#!/bin/ksh93

set -A town_name india pakistan srilanka india
set -A town
m=0
n=0

while [ $m -lt ${#town_name[*]} ]
do
   t1=`echo ${town_name[$m]}`
   town[$n]=$t1
   echo ${town[$n]}
   n=$((n+1)) 
   m=$((m+1))
done

t2=`echo ${town[3]}`
echo $t2

You could also write the loop more compactly as

while (( m < ${#town_name[*]} ))
do
   town[$n]=${town_name[$m]}
   echo ${town[$n]}
   ((n++))
   ((m++))
done

The compact code is giving error
n++ more tokens expected...
first i have tried with the same code but when this error appeared i changed the format.

oops, sorry:



set -A town_name india pakistan srilanka india


m=0

while (( $m < ${#town_name[*]} ))
do
   town[$m]=${town_name[$m]}
   echo ${town[$m]}
   ((m = m + 1 ))
done

echo town=${town[3]}