Array in Ksh

Hi Guys,
My code is something like this

set -A A1 1 7 13 19 
set -A A2 2 8 14 20
set -A A3 3 9 15 21

echo "Enter a number"
read number
for i in 0 2 3 4 
do
 if [ "$number" = "${A1[$i]}" ]
 then
 do something
elif [ "$number" = "${A2[$i]}" ]
 then
 do something
elif [ "$number" = "${A3[$i]}" ]
 then
 do something
fi
done

Actually I have even more array and if - else conditions
my question: is there any other way to do the same thing with out too many conditions.

That depends on if you need to find out the index or not.

If you need the array index of the element matching the input you have to search through the array, though i would prefer some dynamic solution. If your arrays would become a different size you will have to edit the for-loop to reflect this. But "${#arrayname[@]}" (or ${#arrayname[*]}") evaluates to the number of elements in an array an therefore:

typeset -a array 1 2 3 4 5 6
typeset -i index=0

while [ $index -lt ${#array[*]} ] ; do
     print - "Array element $index holds: ${array[$index]}"
     (( index += 1 ))
done

will cycle through all array elements regardless of how many there are.

Note that "*" or "@" as a subscript will always address all array elements, for instance:

typeset -a array a b c d e f g

print - "all array elements: ${array[*]}"
print - "all array elements too: ${array[@]}"

You could use this to "grep" through all elements at once if you only need to know if the input is in the array or not.

There is only one subtle difference between the "*" and the "@" subscript: one evaluates to all elements separated by the IFS (internal field separator) and the other to all elements in one string.

You see the difference here. First store a script called "countargs.sh" and flag it executable:

#!/bin/ksh

typeset -i index=0

print - "Numbers of arguments given: $?"

while [ -n "$1" ] ; do
     print - "argument $index is: \"$1\""
     shift
     (( index += 1 ))
done

exit 0

Now call this script with the following script:

#!/bin/ksh
typeset -a array a b c d 1 2 3

/path/to/countargs.sh ${array[*]}
/path/to/countargs.sh ${array[@]}

I hope this helps.

bakunin

Thank you bakunin...
I think, I didnt ask correctly what i want...

My question about something like two dimensional array.

my code:
set -A A1 1 7 13 19
set -A A2 2 8 14 20
set -A A3 3 9 15 21

what i wanted is, whether i can access the arrays
like

A[$i][$j]

For the ksh language there are no "two-dimensional arrays", hence there are no language devices to handle them. What you try to do is to emulate such a behavior by using part of the name of a variable as index. This is possible but as complicated and limited in its functionality as it sounds.

You will need to use the "eval" keyword for these lines because all the variables are evaluated at the same time and the variables containing the arrays names have to be evaluated before.

typeset -a A1 1 7 13 19
typeset -a A2 2 8 14 20
typeset -a A3 3 9 15 21
typeset -i ArrIdx=1
typeset -i Idx=1

(( ArrIdx = 1 ))
while [ $ArrIdx -le 3 ] ; do
     eval print - "Array A$ArrIdx is \"\${A$ArrIdx[*]}\""   # this is how to address the whole array
     eval print - "Element $Idx is  \"\${A$ArrIdx[\$Idx]}\""   # this is how to address a single element
     (( ArrIdx += 1 ))
done

I hope this helps.

bakunin

ksh93s and later support multidimensional arrays of regular types and compound variables.

ksh93t and later support multidimensional arrays of user defined types.