Array in Ksh


 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications Array in Ksh
# 1  
Old 07-12-2012
Question Array in Ksh

Hi Guys,
My code is something like this

Code:
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.

Moderator's Comments:
Mod Comment edit by bakunin: Please view this code tag video for how to use code tags when posting code and data.

Last edited by bakunin; 07-12-2012 at 06:22 AM..
# 2  
Old 07-12-2012
Quote:
Originally Posted by jeanzibbin
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:

Code:
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:

Code:
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:

Code:
#!/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:

Code:
#!/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
# 3  
Old 07-13-2012
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]
# 4  
Old 07-13-2012
Quote:
Originally Posted by jeanzibbin
My question about something like two dimensional array.
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.

Code:
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
# 5  
Old 07-13-2012
Quote:
Originally Posted by bakunin
For the ksh language there are no "two-dimensional arrays", hence there are no language devices to handle them.
ksh93s and later support multidimensional arrays of regular types and compound variables.

ksh93t and later support multidimensional arrays of user defined types.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning * as value in a ksh array

I want to extract each and single character from a password string and put it in an array. I tried this : set -A password "echo $passwd | awk '{for (i=1; i<=length($1); i++) printf "%s ",substr($1,i,1)}'` It's working as long that the password string doesn't contains any * I tried a few... (5 Replies)
Discussion started by: ce9888
5 Replies

2. Shell Programming and Scripting

[Solved] KSH: Array/If Help

RedHat 5 KSH I am creating an array, and then using case to go through and count for specific words. Then the count gets stored as an expression. string='ftp rcp rsh telnet ftp ftp' set -A myarray $string FTPCOUNT="0" for command in ${myarray} do case $command in ftp) FTPCOUNT=`expr... (2 Replies)
Discussion started by: nitrobass24
2 Replies

3. Shell Programming and Scripting

ksh insert element in array

Hi all, I need help with the following scenario in ksh. If the number of elements contained by arrayA is 11 I need to insert a zero as the element arrayA then print all arrayA elements separated by comma. Appreciate your help. (9 Replies)
Discussion started by: ejianu
9 Replies

4. Shell Programming and Scripting

Array in ksh with if-else

Hi All, My Requirement is as follows: 1. User will input Source Sytem Code as input. 2. I have source system codes as 11, 34, 56, 99, 45 etc. OS Version: SunOS 5.8 Generic_117350-62 sun4u sparc SUNW,Sun-Fire-V890 My code is like... echo 'Source System Code: \c' read varSSCode... (3 Replies)
Discussion started by: saps19
3 Replies

5. UNIX for Dummies Questions & Answers

Need help with KSH Array assignment

The following command is only intermittently successful, depends on the data I give it: set -A ImageShifts_sorted `awk '/START_SECTION IMAGE_DEFINITION/ {getline;getline;getline;getline; print $2"-"$3}' temp.ASCII | sort -u` My Error: set: -1: unknown option Finally, If I run that... (3 Replies)
Discussion started by: nerdcurious
3 Replies

6. UNIX for Advanced & Expert Users

Array copy in ksh

Hi all, Following code in ksh is giving error: fileLimit=5 func(){ dir=&quot;$1&quot; format=&quot;$2&quot; array=&quot;$3&quot; i=0 ls -lrt $format | tail -${fileLimit} | while read f_det; do files=&quot;${f_det},&quot; ((i+=1)) done eval $(echo set -A $array '&quot;${files}&quot;') } func &quot;.&quot; &quot;*.pl&quot; &quot;a&quot; echo... (10 Replies)
Discussion started by: PRKS
10 Replies

7. Shell Programming and Scripting

Ksh array solution.

I was wondering if ksh supported arrays. I have a script that may work with several hosts. I'd like a means of knowing how many hosts I'm working with and an easy way to access them (as variables) in a loop. I'm assuming there's some kind of foreach in shell scripting. (1 Reply)
Discussion started by: mrwatkin
1 Replies

8. Shell Programming and Scripting

using array in ksh

hi all, need help with putting names in an array, i have a few servers which i look up by doing a 'find . -name "*.pid' and the format of the output is like following : ./servername/myserver.pid i was wondering how can i iterate through and store each name in one array my code is... (1 Reply)
Discussion started by: cesarNZ
1 Replies

9. Shell Programming and Scripting

[KSH] Split string into array

Hi, Is there any way to convert a string into an array in KSH? In other words I want to split the string like this: STRING="one two three four" into an array of 4 values splitting on white space. The array should be similar to the one that would be created with the following command: ... (3 Replies)
Discussion started by: piooooter
3 Replies

10. Shell Programming and Scripting

Tokenising into array (KSH)

Greetings all, I've been getting a little frustrated over my scripts as I'm not too experienced with powerful commands such as awk and sed. Hope to find some guidance here. I need to extract the names of all directories within a specified directory, grab their names and then place each name... (5 Replies)
Discussion started by: rockysfr
5 Replies
Login or Register to Ask a Question