untitled

(ff) #1

10.1 Text Transformations 215


In the main body of the program, the hashes are used at the end to compute
the three statistics: count, sum, and sum of squares. First, the month and
year are combined into a single string. This string is then called thekeyfor
the hash value. It is analogous to the index of an array, but the index for
array values can only be an integer, while a hash key can be any scalar. This
includes integers, other numbers, and strings. The value corresponding to
a hash key is specified by using braces as, for example, in the expression
$bmisum{$m}. By contrast, arrays use brackets to specify a value of an array.
The final and concluding part of the program introduces a new kind of
statement: theforeachstatement. This statement is used for performing some
action on every element of a list. This is callediterationorloopingbecause the
same action is done repeatedly, differing each time only by which element is
being processed. In this case the iteration is to be over all of the month-year
combinations that are in the hashes. The body of the iteration should be per-
formed once for each month-year combination. Each time it is performed$m
will be a different month-year combination. The month-year combinations
are the keys of any one of the three hashes. The program uses%count,but
any one of the three hashes could have been used. Thekeysfunction gets
the list of all keys of a hash. This list can be in any order, so one usually sorts
the keys to get output that looks better. If the order does not matter, then one
can omit using the sort function. The rest of the computation is nearly the
same as before except that the values in the hashes are used instead of simple
scalar statistics. Applying this program to the simple four-record example
data file will print the following:


Health Study Data


1/15/2000 18.66 normal 62 cm 46.27 kg 102 lb
1/15/2000 26.93 overweight 63 cm 68.95 kg 152 lb
2/1/2000 33.95 obese 65 cm 92.53 kg 204 lb
2/1/2000 17.38 normal 67 cm 50.35 kg 111 lb


Statistics for 1/2000
Number of records: 2
Average BMI: 22.795
BMI Variance: 34.1964499999997
BMI Standard Deviation: 5.84777308041272


Statistics for 2/2000

Free download pdf