10.1 Text Transformations 225
while (<>) {
chomp;
push(@table, [split]);
}
printstats(1);
printstats(4);
sub stats {
my $count = @table;
my $column = $_[0];
my $sum = 0;
my $sumsq = 0;
for (my $i = 0; $i < $count; $i++) {
my $field = $table[$i][$column];
$sum += $field;
$sumsq += $field * $field;
}
my $mean = $sum / $count;
my $var = ($sumsq - $count * $mean ** 2)
/ ($count - 1);
return ($mean, $var);
}
sub printstats {
my $column = $_[0];
my ($mean, $var) = stats($column);
print("Statistics for column $column:");
print(" mean $mean variance $var\n");
}
Program 10.8 Computing statistics with procedures
•Thereturnstatement marks the end of the computation and specifies
the value produced by the procedure.
10.1.4 Pattern Matching
So far the input files we have considered are record-oriented, that is, each
line has one record and all records have the same structure. Unfortunately,
a large amount of data does not have this simple structure. Consider the