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

Ok. I have the following text file:
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 LINESCORE 24|St. Louis Cardinals|2|7|0|8|0|0|0|2|0|0|0|0|0 17|Cincinnati Reds|3|8|0|5|1|0|0|0|1|1|0|0|x DOUBLES 5046|Craig Paquette|24|St. Louis Cardinals|1|5 5508|Juan Castro|17|Cincinnati Reds|1|1 HOME RUNS 5151|Jim Edmonds|24|St. Louis Cardinals|1|11 3866|Mark McGwire|24|St. Louis Cardinals|1|11 4305|Ken Griffey Jr.|17|Cincinnati Reds|1|8 HOMER DETAILS 5151|Jim Edmonds|24|St. Louis Cardinals|inning 4, 0 out, 0 on off Vill +one 3866|Mark McGwire|24|St. Louis Cardinals|inning 4, 0 out, 0 on off Vil +lone 4305|Ken Griffey Jr.|17|Cincinnati Reds|inning 6, 0 out, 0 on off Bene +s SCORING POSITION 24|St. Louis Cardinals|0|7 17|Cincinnati Reds|1|7 SAC BUNTS 5346|Ron Villone|17|Cincinnati Reds|1 SAC FLIES 5746|Pokey Reese|17|Cincinnati Reds|1 STOLEN BASES 4159|Dante Bichette|17|Cincinnati Reds|1|2 CAUGHT STEALING 5602|Edgar Renteria|24|St. Louis Cardinals|1|3 PITCHING 24|St. Louis Cardinals 4379|Andy Benes|L, 2-2|0|6|8|3|3|0|0|1|24|31|41|72|4.41|.258 4625|Heathcliff Slocumb|0|0|1|0|0|0|0|0|0|4|3|8|11|4.08|.231 4979|Mike Mohler|0|0|1|0|0|0|1|3|0|4|8|9|17|8.79|.293 17|Cincinnati Reds 5346|Ron Villone|W, 3-1|0|6.2|5|2|2|3|4|2|28|42|59|101|4.73|.301 6174|Scott Williamson|SV, 2|0|2.1|2|0|0|0|3|0|9|10|25|35|2.53|.211 DOUBLE PLAYS 24|St. Louis Cardinals|2|Vina to Renteria to McGwire|Vina to Renteria +to McGwire HBP 24|St. Louis Cardinals|1|Tucker by Slocumb 17|Cincinnati Reds|1|Vina by Villone WP 4979|Mike Mohler|24|St. Louis Cardinals|1 UMPIRES Miller|Runge|Shulock|Rapuano TIME 2:31 ATTENDANCE 42,126
and I have the following Perl code:
open(DATA,"c:/MLB_boxscore.TXT") or die "Can't open file"; print "<html><head><title>My page</title></head><body>"; print "<table>"; $linescore = 0; while(<DATA>) { if($linescore) { last if /^\s*$/; chomp; push @LS, split('\|',$_); print "<tr>"; } elsif(/^LINESCORE/) { $linescore = 1 } { print "<td>$el @LS </td>"; print "</tr>"; } } print "</table></body></html>"; close INPUT;
I am trying to call out the information under the heading LINESCORE and present it with St Louis information on line 1 and Cinci's info on Line 2 (in an HTML table). My current output looks like this:
24 St. Louis Cardinals 2 7 0 8 0 0 0 2 0 0 0 0 0 24 St. Louis Cardinals 2 7 0 8 0 0 0 2 0 0 0 0 0 17 Cincinnati Reds 3 +8 0 5 1 0 0 0 1 1 0 0 x
Ideas? Thanks.

Replies are listed 'Best First'.
Re: Picking out text
by Masem (Monsignor) on Apr 11, 2001 at 20:39 UTC
    You need to reset your @LS array between readings (that's why the St. Louis data is still there when you get the Cinninnati data). Either add "@LS = ();" prior to reading the linescore line, or better yet, use "my @LS;" at the same point.

    (Also, if you are going to post formatted code, use the <CODE> tags to make it easier on yourself.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Picking out text
by suaveant (Parson) on Apr 11, 2001 at 20:49 UTC
    a couple things... looks like my code anyway :)
    do push @LS, [split('\|',$_)]; then your @LS with be an array of arrays... pushing a split onto an array puts all your data onto the array each as new elements...

    then to print the data out separately you can do...

    for(@LS) { #go through @LS 1 by 1, putting array ref into $_ print "<TD>$el @{$_}</TD>"; #print each line converting arrayref int +o array for printing ## or you could do print "<TD>$_->[1]</TD>";# to print just the team name... etc }
    Update:
    Actually, for what you are doing I would just do...
    my @line = split('\|',$_); print "<TR><TD COLSPAN=10>Team: $_->[1] Score: $_->[0]</TD></TR>"; print "<TR><TD><B>Innings</B></TD>"; for(my $i = 2; $i <= 11; $i++) { print "<TD>$_->[$1]</TD>"; } print "</TR>";
    where you now have the push @LS code... would work just as well.

    ...or something similar, I don't know if I am right about the data, and you may not want to display it that way... but it's just an example
                    - Ant