use strict;
use CGI ':standard';
use HTML::Table; # I like this module for simple tables
open(INPUT,"stats.txt") or die "Can't open file";
print start_html('Box Score');
$/ = ""; # Read data a paragraph at a time
while (my $players = ) {
WritePlayers($players);
# Grab all chunks of data in a loop!
my $totals = ;
WriteTotals($totals);
}
close INPUT;
sub WritePlayers { # Renamed for clarity
my $table = HTML::Table->new;
my $old_id = '';
# Split the chunk into lines and loop
foreach my $player (split(/\n/,$_[0])) {
# Split the line into fields
my ($id, undef, $name, $pos, @others) = split(/\|/, $player);
# Add the data to my HTML table
my $prefix = ($id eq $old_id) ? '_' : '';
$table->addRow("$prefix$name, $pos", @others);
$old_id = $id;
}
# Table formatting
$table->setColWidth(1, 150);
$table->setColWidth($_, 20) foreach (2..11);
$table->print;
print p;
}
sub WriteTotals { # Renamed for clarity
my $table = HTML::Table->new;
# Just split this small chunk into fields
my @fields = split(/\|/, $_[0]);
# Add the fields to my HTML table
$table->addRow(@fields);
# Table formatting
$table->setColAlign($_, 'CENTER') foreach (1..3);
$table->print;
print p;
}