untitled

(ff) #1

208 10 Transforming with Traditional Programming Languages


print "Health Study Data\n\n";

while (<>) {
$month = 0 + substr $_, 0, 2;
$day = 0 + substr $_, 2, 2;
$yr = 0 + substr $_, 4, 2;
$year = $yr < 20? 2000 + $yr : 1900 + $yr;
$bmi = 0 + substr $_, 6, 8;
$status = "normal";
$status = "obese" if (substr $_, 14, 3) > 0;
$status = "overweight" if (substr $_, 17, 3) > 0;
$height = 0 + substr $_, 20, 3;
$wtkgs = 0 + substr $_, 23, 8;
$wtlbs = 0 + substr $_, 31, 3;
print "$month/$day/$year $bmi $status";
print " $height cm $wtkgs kg $wtlbs lb\n"
}

Program 10.2 Alternative version of program 10.1

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

Number of records: 4
Average BMI: 24.23
BMI Variance: 59.9052666666668
BMI Standard Deviation: 7.73984926640479

This program uses all three parts of a typical program. The first part prints
the title of the report as before, and the body processes the records in the
health study file, but now there is also a concluding part that prints the
statistics. The processing of the records has some additional computations.
Thecountvariable has the number of records, thebmisumvariable has the
sum of the BMI values for all records, and thebmisumsqhas the sum of the
squares of the BMI values. These are set to 0 in the introductory part of the
Free download pdf