CIS 201: Linux Shell Scripting

lewing@isc.tamu.edu

Gabelli School of Business, CIS Department

| CISWEB Doug White | RWU Main | White Hat Research | Email Doug | Access Data |

CIS 201: Help on Arrays

For several problems you need multi-dimensional arrays to help out. This is not really possible in ksh but there is a way around it. In C++, there is a command called a struct which allows you to create custom variables. In ksh, it's called a compound variable. This is a variable which YOU define so it can be anything. This also means you can make an array of CVs just like you can make an array of structs. The syntax is messy but it works really well for handling things like horses with times so you don't have to do weird things with var names.

The syntax to declare this is like this:

thisType=(

var1

var2

)

where thisType is the name you want and var1 and var2 are containers inside the CV.

Here is a more complicated one

coolVar=(

integer nums

name

float times[4]

}

That is now a type that has an array inside it. So watch

coolVar[25]

creates an array of coolVar with 25 rows. Each row has a var called times which has four cells in it. Voila' a two dimensional array in ksh.

The syntax to access this is like the following

echo ${coolVar[1].times[1]}

or

echo ${coolVar[$n].times[$y]}

Would print the time of item 1 time 1 in the first example and item n time y in the second.

Now a loop

for (( x=0; $x<25; x++)) ; do

coolVar[$x].nums=0

coolVar[$x].name=" "

for (( y=0; $y<4; y++)) ; do

coolVar[$x].times[$y}=0

done

done

This initializes all the cells to zeros or blanks.

 

| Contact | © 2005 Secure Technology, LLC. |