Perl Newby has asked for the wisdom of the Perl Monks concerning the following question:

Synopsis: What I am doing is taking a text file that is pipe delimited and parsing the data out. From this I want to take the data and put it into an HTML format so that it can be viewed on the web. So far I have been able to come up with a couple of sub routines that actually accomplish this task. In the players section I had to use the else statements in order to pull all the info for each player. Since I am new to PERL I figured that I have probably declared too many variables and written too much code for what I am trying to accomplish. I was wondering if there was a way I could actually shorten the code more. The following is what the text file looks like:
24|St. Louis Cardinals 1|4995|Fernando Vina|2B|4|0|0|0|0|0|0|.317|.397|.425 2|5602|Edgar Renteria|SS|4|0|1|0|1|0|1|.260|.321|.470 3|5151|Jim Edmonds|CF|3|1|2|1|5|1|0|.409|.538|.864 4|3866|Mark McGwire|1B|3|1|1|1|4|1|0|.323|.494|.903 5|4547|Ray Lankford|LF|4|0|0|0|0|0|3|.193|.297|.420 6|5046|Craig Paquette|3B|3|0|1|0|2|1|1|.264|.325|.486 7|6117|J.D. Drew|RF|4|0|1|0|1|0|1|.327|.462|.596 8|5205|Mike Matheny|C|3|0|0|0|0|0|0|.289|.375|.421 8|4506|Thomas Howard|PH|1|0|1|0|1|0|0|.280|.357|.760 8|5897|Eli Marrero|PR|0|0|0|0|0|0|0|.250|.343|.643 9|4379|Andy Benes|P|2|0|0|0|0|0|0|.167|.167|.167 9|3625|Eric Davis|PH|1|0|0|0|0|0|1|.281|.388|.491 9|4625|Heathcliff Slocumb|P|0|0|0|0|0|0|0|.000|.000|.000 9|4979|Mike Mohler|P|0|0|0|0|0|0|0|1.000|1.000|1.000 9|5880|Larry Sutton|PH|1|0|0|0|0|0|0|.000|.000|.000 TOTALS|33|2|7|2|14|3|7 17|Cincinnati Reds 1|5746|Pokey Reese|2B|3|1|1|1|1|0|0|.343|.408|.429 2|5930|Sean Casey|1B|3|0|1|0|1|1|0|.200|.344|.320 3|4305|Ken Griffey Jr.|CF|4|1|2|2|5|0|1|.210|.326|.448 4|5699|Dmitri Young|LF|4|0|1|0|1|0|1|.287|.339|.446 4|5540|Alex Ochoa|LF|0|0|0|0|0|0|0|.297|.357|.595 5|4615|Eddie Taubensee|C|4|0|0|0|0|0|1|.325|.387|.542 6|4159|Dante Bichette|RF|3|0|2|0|2|0|0|.219|.265|.324 6|6174|Scott Williamson|P|0|0|0|0|0|0|0|-|-|- 7|5838|Aaron Boone|3B|3|0|0|0|0|0|0|.270|.389|.438 8|5508|Juan Castro|SS|3|1|1|0|2|0|0|.333|.333|.667 9|5346|Ron Villone|P|1|0|0|0|0|0|0|.222|.222|.222 9|5299|Michael Tucker|RF|0|0|0|0|0|0|0|.167|.375|.500 TOTALS|28|3|8|3|12|1|3
This is the code that I have written so far:
open(INPUT,"c:/MLB_boxscore.TXT") or die "Can't open file"; print "<html><head><title>Box Score</title></head><body>"; print "<table>"; use strict; my $insection = ""; $y = 1; $l = 0; $/ = ""; $players1 = <INPUT>; $totals1 = <INPUT>; $players2 = <INPUT>; $totals2 = <INPUT>; $players = $players1; $totals = $totals1; players($players); totals($totals); print "\n\n"; $players = $players2; $totals = $totals2; players($players); totals($totals); print "</table></body></html>"; close INPUT; sub chomped{ $/ = "\n"; chomp; @LS = (); push @LS, split('\n',$player); return @LS; } sub players{ $players = $players; print "<table>"; foreach $player($players){ chomped($player); foreach $LS(@LS){ chomp; @SL = (); push @SL, split('\|',$LS); for ($x=0;$x<=$#SL;$x++){ if ($x == 0){ $id = $SL[$x]; if ($id == $oldid){ print "<tr><td width='150'>_"; } else { print "<tr><td width='150'>"; } $oldid = $id; } elsif ($x == 1){ next; } elsif ($x == 2){ print $SL[$x]; } elsif ($x == 3){ print ", " . $SL[$x] . "</td>"; } elsif ($x == 13){ print "<td width='20'>" . $SL[$x] . "</td></tr>"; } else { print "<td width='20'>" . $SL[$x] . "</td>"; } } } } } sub totals{ $totals = $totals; foreach $total($totals){ chomp; @LS = (); push @LS, split('\|',$total); print "<tr>"; print "<td>" . $LS[$_] . "</td>" for(0..$#LS); print "<td align='center'>-</td>" for(0..2); print "</tr></table>"; } }

Replies are listed 'Best First'.
Re: Running a Sub from a Text File 2
by buckaduck (Chaplain) on Apr 20, 2001 at 02:25 UTC
    Here's some suggestions, which utilize CGI and HTML::Table for clarity. Some simplification was possible since leading/trailing whitespace won't matter in an HTML document anyway:
    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 = <INPUT>) { WritePlayers($players); # Grab all chunks of data in a loop! my $totals = <INPUT>; 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; }

    Note that you can easily turn this into a CGI program by adding print header; before the print start_html(); line.

    buckaduck

Re: Running a Sub from a Text File 2
by plazm (Initiate) on Apr 20, 2001 at 00:35 UTC
    The simplest way to output HTML is to use fast templates. This will keep code aside your HTML and let you control the layout separately. A.