untitled

(ff) #1

214 10 Transforming with Traditional Programming Languages


while (<>) {
chomp;
@record = split(" ", $_);
@date = split("/", $record[0]);
$month = $date[0] + 0;
$day = $date[1] + 0;
$yr = $date[2] + 0;
if ($yr < 20) { $year = 2000 + $yr; }
elsif ($yr < 1000) { $year = 1900 + $yr; }
else { $year = $yr; }
$bmi = $record[1];
if ($record[2] + 0 > 0) { $status = "obese"; }
elsif ($record[3] + 0 > 0) { $status = "overweight"; }
else { $status = "normal"; }
$height = $record[4] + 0;
$wtkgs = $record[5] + 0;
$wtlbs = $record[6] + 0;
print("$month/$day/$year $bmi $status");
print(" $height cm $wtkgs kg $wtlbs lb\n");
$m = "$month/$year";
$count{$m}++;
$bmisum{$m} += $bmi;
$bmisumsq{$m} += $bmi ** 2;
}
foreach $m (sort(keys(%count))) {
print("\nStatistics for $m\n");
print("Number of records: $count{$m}\n");
$bmimean = $bmisum{$m} / $count{$m};
print("Average BMI: $bmimean\n");
$bmivar = ($bmisumsq{$m} - $count{$m} * $bmimean ** 2)
/ ($count{$m} - 1);
print("BMI Variance: $bmivar\n");
$bmisd = $bmivar ** 0.5;
print("BMI Standard Deviation: $bmisd\n");
}

The hashes are used in each of the three parts of the program. In the intro-
ductory part, the three hashes are declared and initialized to empty hashes.
As in the case of scalars, it is not necessary to declare and initialize hashes. If
they are not declared and initialized, then they will be set to empty hashes.
Arrays also do not have to be initialized. By default, arrays are initially
empty.
Free download pdf