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

I have a text file that is similar to this:
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
I am trying to use the following code to pull out everything from the file except the PITCHING information. I have been unable to get all of the information I need. Any suggestions?
open(INPUT,"c:/MLB_boxscore.TXT") or die "Can't open file"; print "<html><head><title>My page</title></head><body>"; print "<table>"; my $insection = ""; while (<INPUT>) { if (/^SAC\s+BUNTS/) { $insection="sac bunts" } if (/^SAC\s+FLIES/) { $insection="sac flies" } if (/^STOLEN\s+BASES/) { $insection="stolen bases" } if (/^CAUGHT\s+STEALING) { $insection="caught stealing" } if (/^PITCHING/) { $insection="pitching" } if(defined($oldsection) && $insection ne $oldsection) { undef $oldsection; } elsif (defined($oldsection) && $insection eq $oldsection) { if ($insection == "scoring position"){ last if /^([A-Z][A-Z\s]*)/; chomp; @LS = (); push @LS, split('\|',$_); print "<tr>"; print "<td>" . $LS[$_] . "</td>" for (1..$#LS); print "</tr>"; } } elsif ($insection && !defined($oldsection)) { $oldsection = $insection; next; } } print "</table></body></html>"; close INPUT;

Replies are listed 'Best First'.
Re: Pulling Values out of a File
by astanley (Beadle) on Apr 13, 2001 at 22:40 UTC
    Your trouble lies in the line: if ($insection == "scoring position") {. First of all I don't see a SCORING POSITION option either in your $insection= statements or in your sample data. However to answer your question you want to modify that if line to look like this:
    if ($insection ne "pitching") {

    That will skip the print <td> ONLY when you are inside the PITCHING subsection. HTH!

    -Adam Stanley
    Nethosters, Inc.
Re: Pulling Values out of a File
by thabenksta (Pilgrim) on Apr 13, 2001 at 22:34 UTC

    Could you please explain your problem a little more?

    my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';
      I just want to pull out everything from the file except the pitching information. I want to be able to pull this info out and output it to an html format.

        Yes i see, but what is happening, what is not working about it, are you getting errors, is it working fine but it still gives you the pitching info, what is the problem exactly?

        When asking a question it helps when you give more details of the problem, that way I dont have to totally figure out what you are trying to do, I can just focus on the main problem.

        my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';