10.2 Transforming XML 251
use Template;
while (<>) {
$interviews[$i]{month} = substr($, 0, 2) + 0;
$interviews[$i]{day} = substr($, 2, 2) + 0;
$interviews[$i]{year} = 2000 + substr($, 4, 2);
$interviews[$i]{bmi} = substr($, 6, 8) + 0;
$status = ’normal’;
if (substr($, 14, 3) + 0 > 0) { $status = ’obese’; }
if (substr($, 17, 3) + 0 > 0)
{ $status = ’overweight’; }
$interviews[$i]{status} = $status;
$interviews[$i]{height} = substr($, 20, 3) + 0;
$interviews[$i]{weight} = substr($, 23, 8) + 0;
$i++;
}
$tt = new Template;
$vars = {
HealthStudyInterviews => [@interviews],
};
$tt->process("health.tt", $vars);
Program 10.22 Perl program that uses a template
[% FOREACH i IN HealthStudyInterviews %]
Height=’[% i.height %]’ Weight=’[% i.weight %]’/>
[% END %]
Template 10.2 Perl template to convert fixed-width fields to variable-width fields