I have a text file that I am trying to run a sub on to get the data out. I just want to be able to get the following out of the file and stop after the second totals. I have more data below that but I just need the first four chunks. I have begun writing a sub but seem to have hit a wall. Any suggestions would be appreciated.
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
I am trying to use the following code to get this information out of the file. Like I said above, there is a lot of data in the file. Although, I just need the first four sections.
open(INPUT,"c:/MLB_boxscore.TXT") or die "Can't open file"; print "<html><head><title>Box Score</title></head><body>"; print "<table>"; my $insection = ""; my $hr, $db, $hrd, $y, $l, $linescore; $hr = 0; $db = 0; $hrd = 0; $y = 0; $l = 0; $v = 0; $h = 0; $linescore = 0; while (<INPUT>) { if ($y eq 4){$linescore = -1} else {$linescore = 0} if ($linescore eq 0){ linescore($y, $l); $y = $x; } if ($linescore ne 0){ sub linescore { if (/^\s*$/) { $l = 0; $y += 1; $b = 0; } else { if ($y eq 0){ players($y, $l); $v_team = $team; $b = $b; $l = $l; $v_line[$b] = $line; return $v_line[$b], $l; } if ($y eq 1){ totals($y); $b = $b; $v_total[$b] = $total; return $v_total[$b]; } if ($y eq 2){ players($y); return $h_team = $team; $b = $b; return $h_line[$b] = $line; } if ($y eq 3){ totals($y); $b = $b; return $h_total[$b] = $total; } } $x = $y; return $x, $l; } sub players{ if ($l eq 0){ chomp; @LS = (); push @LS, split('\|',$_); $team = $LS[0]; $l = 1; return $team, $l; } if ($l ne 0){ print "mike"; my @linescores = (0,2,3,4,5,6,7,8,9,10,11,12,13); chomp; @LS = (); push @LS, split('\|',$_); my $line = ""; for ($i=0;$i<$#LS;$i++){ $a = $linescores[$i]; $linescore[$i] = $LS[$a]; print $linescore[$i]; } my $line = $line . "<tr bgcolor='#CCCCCC'>"; for ($x=1;$x<=$#linescore;$x++){ $line = $line . "<td>" . $linescore[$x] . "</td>"; $linescore[$x] = undef; } $line = $line . "</tr>"; $b += 1; return $line, $b; } } sub totals{ my @totals = (0,4,5,6,7,8,9,10); chomp; @LS = (); push @LS, split('\|',$_); my $total = ""; for ($i=0;$i<$#LS;$i++){ $a = $totals[$i]; $tot[$i] = $LS[$a]; } my $total = $total . "<tr bgcolor='#CCCCCC'>"; for ($x=1;$x<=$#tot;$x++){ $total = $total . "<td>" . $tot[$x] . "</td>"; $tot[$x] = undef; } $total = $total . "</tr>"; $b += 1; return $total, $b; } print "</table>"; print "</table></body></html>"; close INPUT;

In reply to Running a Sub for a Text File 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.