untitled

(ff) #1

10.1 Text Transformations 211


print "Health Study Data\n\n";


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");
}


Program 10.4 Reformatted health study data

Program 10.4 does the same transformation as program 10.1, except that
it assumes that the data file uses variable-length fields separated by spaces,
and that the month, day, and year in a date are separated from one another
by using the forward slash (“/”) character. This program will transform the
following data file:


1/15/2000 18.66 0 0 62 46.27 102
1/15/2000 26.93 0 1 63 68.95 152
2/1/2000 33.95 1 0 65 92.53 204
2/1/2000 17.38 0 0 67 50.35 111


to the following:


Health Study Data

Free download pdf