in reply to Parsing a Text File

Well, Newby... you've been throwing this one around a lot :)

its obvious you want a bunch of this data... you could do something like this.

my %data; my $latest = ''; while(<INPUT>) { chomp; if(/^([A-Z]+)\s*/) { #a line with all caps... new section $latest = $1; #what section are we in next; #go to next line } if($latest) { ## Next line handles $data{$latest} as an anonymous ## array, allowing you to store all the lines for ## each section that way push @{$data{$latest}}, $_; #just puts the line in, you could proc +ess it somehow } }
Now $data{PITCHING} holds a ref to an array of all the lines in the PITCHING section... etc, etc... that help?
                - Ant

Replies are listed 'Best First'.
Re: Re: Parsing a Text File
by buckaduck (Chaplain) on Apr 12, 2001 at 01:21 UTC
    if(/^([A-Z]+)\s*/)
    The final  \s* is meaningless here; it will always be true.

    Also, based on the data set, he may want to use whitespace in his hash keys. How about this?

    if (/^([A-Z][A-Z\s]*)/)
    I decided to make sure that the first character is uppercase, but allow whitespace after that.

    buckaduck

      Opps, I meant...
      if(/^([A-Z]+)\s*$/)
      To prevent a line starting with a capitol letter but having other stuff... but to allow a whitespace after the marker, just in case.
                      - Ant