untitled

(ff) #1

206 10 Transforming with Traditional Programming Languages


print("Health Study Data\n\n");

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

Program 10.1 Convert fixed-width fields to variable-width fields

One of the ways that this transformation can be accomplished is shown
in program 10.1. This program has only two parts: an introduction and a
body. It does not have any concluding part. Examples of programs that have
a concluding part are given later.
The first or introductory part of the program prints the title of the report.
The ā€œ\nā€ is called anewline; it ends the line at that point. Two newlines in a
row will insert an extra blank line between the title and the rest of the report.
The body of this program performs the same operation on all of the lines of
the file to be transformed. Thewhilestatement means that everything in the
braces is to be repeated as long as the condition (in parentheses) is true. The
<>angle brackets are used to get the next line of the input file. It succeeds
as long as there is another line, and it fails (i.e., it tells the while statement
that it should stop) when there is nothing left in the file.
The statements in the transformation block usevariables. The variables
have names such asmonth,day. The dollar signs are not part of the name
Free download pdf