|
|
November
24, 2003 -
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.
|
|
|
|
|
|
|
|
|
|
|
|
Learn
more...
If you are new to the UNIX or Linux
operating system and would like to learn
more, you may want to consider
registering for LiveFire Labs' UNIX
and Linux Operating System Fundamentals
online training course.
If you already have a solid grasp of the
fundamentals but would like to learn more
about the Korn shell and basic and
advanced shell scripting, taking our Korn
Shell Scripting course will be
beneficial to you.
Our
innovative hands-on training model allows
you to learn
UNIX by completing hands-on
exercises on real servers in our Internet
Lab.
More
Tips...
· Popular
UNIX Tips from the Past
|
|
|
|
 |
 |
| |
Receive
the UNIX Tip, Trick, or Shell Script of the
Week by Email
|
|
|
|