"Taking a LiveFire Labs' course is an excellent way to learn
Linux/Unix. The lessons are well thought out, the material is
explained thoroughly, and you get to perform exercises on a real
Linux/Unix box. It was money well spent."
Ray S.
Pembrook Pines, Florida
LiveFire Labs' UNIX and Linux Operating System Fundamentals
course was very enjoyable. Although I regularly used UNIX systems
for 16 years, I haven't done so since 2000. This course was a
great refresher. The exercises were fun and helped me gain a real
feel for working with UNIX/Linux OS. Thanks very much!"
Ming Sabourin
Senior Technical Writer
Nuance Communications, Inc.
Montréal, Canada
Read
more student testimonials...
Receive UNIX Tips, Tricks, and Shell Scripts by Email
LiveFire Labs' UNIX Tip,
Trick, or Shell Script of the Week
Korn Shell Arrays - Getting Creative - Part III
For the past two weeks you've been shown how
to assign and access values to individual positions within an array.
What if you wanted to store and then retrieve/manipulate multiple
attributes of a large number of similar objects without giving up the
performance achieved from using arrays? This tip will present one
possible option.
Consider what could be accomplished when using two or more single
dimension arrays and a "master" subscript/index to tie the same
element position in each array to one object that belongs to a large
group of similar items. Clear as mud? Maybe an example will help.
The following data set was selected to illustrate this concept without
complicating things. In reality, the data you use will most likely be
more technical and obviously larger in quantity.
Peter M 22 brown green
Paul M 27 blonde blue
Mary F 20 red green
We'll pick up after five individual arrays (name, gender, age, hair,
and eyes) have been populated with the above data. This may have been
accomplished by using a while loop to extract the data from one or
more files and then store it in the appropriate array.
In this first snippet of code, the master index is set to the first
object (person in this case), and then the attributes for each person
is displayed:
$ MASTER_INDEX=0
$ while [ ${MASTER_INDEX} -lt ${#name[*]} ]
> do
> print ${name[${MASTER_INDEX}]}
> print ${gender[${MASTER_INDEX}]}
> print ${age[${MASTER_INDEX}]}
> print ${hair[${MASTER_INDEX}]}
> print ${eyes[${MASTER_INDEX}]}
> (( MASTER_INDEX=MASTER_INDEX+1 ))
> done
Peter
M
22
brown
green
Paul
M
27
blonde
blue
Mary
F
20
red
green
$
Each time through the loop, the master index (subscript) is
incremented by 1 until all objects have had each of their attributes
displayed.
This second loop will calculate the total age of the group, which is
then averaged and displayed:
$ MASTER_INDEX=0
$ TOTAL_AGE=0
$ while [ ${MASTER_INDEX} -lt ${#name[*]} ]
> do
> (( TOTAL_AGE=TOTAL_AGE+${age[${MASTER_INDEX}]} ))
> (( MASTER_INDEX=MASTER_INDEX+1 ))
> done
$ (( AVERAGE_AGE=${TOTAL_AGE}/${#name[*]} ))
$ print ${AVERAGE_AGE}
23
$
Although these are relatively basic examples of how to use this
technique, you should consider how it might be used to rapidly
manipulate large quantities of data without incurring the overhead of
disk I/O.