ksh script - arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh script - arrays
# 1  
Old 06-22-2005
ksh script - arrays

hello all.. I have been browsing / searching through the forum and have yet not been able to find what I was looking for.

I am fairly new to ksh and my task is to create a function that reads in an input file:

*****************
2

3
1

abc
def
ghi

/dev/sid/
*****************
(NOTE: the spaces between data sets above DOES NOT exist but it's only there for ease of understanding)

the first line (2 in this case) consists of the numbert of data sets followed by the number of parameters in the 2 datasets (3 and 1).

(abc,def,ghi) are the parameter values for the first data set and (/dev/sid) is tha value for the second data set.

The above 2 data sets need to be scanned into different arrays and hence this is what i came up with:

cat ${INPUT_FILE} | while read LINE #processing steps for all common parameters, assign each common parameters to the array variable COMMON_PARAMETER_CONTENT
do

if [[ $count -le $PARAMETER_CONTENT ]]
then
SUBSET_ARRAY[count]=$LINE
((count+=1))
continue
fi

i = 0
countA = 0
countB = 0

while [[ $countB -l (SUBSET_ARRAY[i]) ]] //need a check here to prevent overflowing..!
do
while [[ $countA -le (SUBSET_ARRAY[i]) ]]
do
[I] SUBSETDATA + "i" = $LINE
((countA+=1))
((DATA_TOTAL_LINE+=1))
done
((i+=1))
countA = 0
done

My question is.. will the highlighted, Italicised line work? Can i loop thru' and put the diff datasets into diff arrays using the "string join" ? Thanks in advance and sorry for the long post.

Sid
# 2  
Old 06-22-2005
That line is illegal with all versions of ksh. Working with dynamically named arrays is hard. But try this..
Code:
#! /usr/bin/ksh


integer ndataset idataset nline
exec < sid.dat

#  Read in the number of datasets (first line)
read ndatasets
echo ndatasets = $ndatasets

#  read in the sizes of all of the datasets
echo
idataset=0
while ((idataset < ndatasets)) ; do
        read datasetsize[idataset]
        ((idataset=idataset+1))
done
idataset=0
while ((idataset < ndatasets)) ; do
        echo size of datasize $idataset is ${datasetsize[idataset]}
        ((idataset=idataset+1))
done

#  read in the datasets
idataset=0
while ((idataset < ndatasets)) ; do
        nline=0
        while ((nline < ${datasetsize[idataset]})) ; do
                read inputline
                eval dataset$idataset[nline]=\"\$inputline\"
                ((nline=nline+1))
        done
        ((idataset=idataset+1))
done

#  display the datasets
idataset=0
while ((idataset < ndatasets)) ; do
        nline=0
        while ((nline < ${datasetsize[idataset]})) ; do
                eval echo dataset = $idataset \"   \" line = $nline \${dataset$idataset[nline]}
                ((nline=nline+1))
        done
        ((idataset=idataset+1))
done
exit 0

And a sample run...
Code:
$
$ cat sid.dat
2
3
1
abc
def
ghi
/dev/sid/
$ ./sid
ndatasets = 2

size of datasize 0 is 3
size of datasize 1 is 1
dataset = 0   line = 0 abc
dataset = 0   line = 1 def
dataset = 0   line = 2 ghi
dataset = 1   line = 0 /dev/sid/
$

# 3  
Old 06-23-2005
Perderabo : Thank you very much..! Smilie That is what I was looking for. Once again really appreciate your help.

Sid
# 4  
Old 06-27-2005
Another question.

Hi,

Another quick/noob question I have:

Can ksh/ ksh 93 support multi-dimensional arrays more specifically 2D arrays?

I THINK they cannot be supported but I need a bit more info on that. Any help will be appreciated.! Thanks in advance.

Sid Smilie

Sorry guys / gals... although I couldn't find the answer to the above using the "search" function on here, I managed to find it by googling it:

https://www.unix.com/archive/index.php/t-2054.html

Last edited by sidamin810; 06-27-2005 at 12:21 PM.. Reason: Found the answer on here
# 5  
Old 06-27-2005
No, multidimensional arrays are not supported. Still it is easy to organize several arrays to work as one table using the index as "key":

Code:
aName[1]="John" ; aPhoneNr[1]="123"
aName[2]="Jack" ; aPhoneNr[2]="234"
aName[3]="Theo" ; aPhoneNr[3]="345"
aName[4]="Dick" ; aPhoneNr[4]="456"

# searching for Theos Phone number:

search="Theo"

(( cnt = 1 ))
while [ $cnt -le ${#aName[@]} ] ; do
     if [ "${aName[$cnt]}" = "$search" ] ; then
          print "found the Name ${aName[$Cnt]}, his nr is ${aPhoneNr[$cnt]}"
     fi
done

bakunin
# 6  
Old 07-04-2005
Error Debugging my ksh script.

Thank you Bakunin..! Appreciate your help greatly.. Smilie

I finally figured out the error. It had to be with the max. number of brackets that nedit could support.

APOLOGISE for the long post which I deleted now that the problem has been solved.

Last edited by sidamin810; 07-05-2005 at 03:09 PM.. Reason: debug question.
# 7  
Old 07-12-2005
Need some clarification.

POsted below is the "modified" function from the above program, that I've been working on.

I needed some clarification for the highlighted code below. Does

arr$idataset$nline[$i]=${arr[$i]} this mean, that every time the data/line is read in, it gets stored in a distinct array specified by the $idataset, $nline AND the $i values? Or it just "echo's" that format to the screen..? I would like it to save the elements in the distinct arrays. Any help will be appreciated. Thanks in advance.


# read in the datasets into distinct arrays specified by the dataset location
idataset=0
typeset -i i=0 # to give this variable a local scope
while ((idataset < ndatasets)) ; do
nline=0
while ((nline < ${datasetsize[idataset]})) ; do
read inputline
IFS=,;set -A arr ${inputline}
while (( $i < ${#arr[@]} )); do
set -A arr$idataset$nline
arr$idataset$nline[$i]=${arr[$i]}

((i+=1))
done
((i=0))
eval dataset$idataset[nline]=\"\$inputline\"
((nline=nline+1))
done
((idataset=idataset+1))
done
}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh arrays

Hi, I have a ksh script in which I need to fill an array with a list of filenames. It currently works like this: set -A array \ val1 \ val2 \ val3However, I was wondering why it's not possible to do something like this to make it easier to parse values to the array: set -A array... (3 Replies)
Discussion started by: Subbeh
3 Replies

2. Shell Programming and Scripting

Multidimentional arrays in KSH

Hi, I am using KSH shell and need to define a multi dimensional array in which i need to store and retrive data. Please provide your valuable in puts. Eg: asbjkasd 1234 asdhnas 1254 i need to store the above values and use them as input to another command. Note each line is a pair. Thanks... (8 Replies)
Discussion started by: hraj1984
8 Replies

3. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

4. Shell Programming and Scripting

KSH: Reading a file line by line into multiple arrays

Hi - I have a file that contains data in this format:- #comment value1 value2 value3 #comment value4 value5 value6 value7 #comment value8 value9 I need to read value1, value2 and value3 into one array, value4 value5 value6 and value7 into another array and value8 and value9 into a 3rd... (2 Replies)
Discussion started by: sniper57
2 Replies

5. UNIX for Dummies Questions & Answers

Arrays in nawk and ksh

I'm not confident at all on how arrays work. I want to know how to set arrays in ksh and in nawk (is there a difference??) if someone can show me some examples of both that will be great. Tried to look up on the net but was confusing me more. Any help would be appreciated. (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

6. Shell Programming and Scripting

Export Arrays in ksh

Hello everybody! Why I can export arrays in ksh? I trie this for exemplo: In parent script array=a array=b array=c export array When I see the variable array in child script there are only first index. For exemplo in child script +echo ${array} a (3 Replies)
Discussion started by: ricardo.ludwig
3 Replies

7. Shell Programming and Scripting

List of arrays in ksh?

Hi all, In a loop, i am creating an array of integer and adding it to another array that hold arrays // psuedo code pttrnArray=$type$data // This is array of integers. pttrnArrays=${pttrnArray} // This is supposed to be array of arrays. But when i print pttrnArrays, i get only the... (1 Reply)
Discussion started by: jakSun8
1 Replies

8. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies

9. Shell Programming and Scripting

Automatic Arrays in ksh

Given a line of text in ksh: string1 string2 string3 .....stringn is there a way of automatically assigning each string to an array element? Or just different variables would do. Thanks, Jon (1 Reply)
Discussion started by: Jonny2Vests
1 Replies

10. Shell Programming and Scripting

KSH and arrays

hello all, I browsed the forum (briefly) and I am having issues with a script I writing. I need to check a directory and see if there are files there, if so process all of them. The issues I am having is that when I create the array of files names using set -A filenames "$(ls -1... (1 Reply)
Discussion started by: whited05
1 Replies
Login or Register to Ask a Question