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>"; } }

In reply to Running a Sub from a Text File 2 by Perl Newby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.