in reply to Parse Loops with flat text files. (code)
A couple of points. First of all, your return logic seems off in your two test subs. Hazarding a guess, I'd say you just wrapped the foreach loops around your previous version that dealt with only one pattern -- but now a line must match *all* the patterns to succeed (it'll return 'undef' if any don't match). I'm thinking you want isbeg() (and isend()) to return true if *any* one of the re's matches the line:
sub isbeg { my $test = shift; foreach (@beginnings) { return 1 if $test =~ /$_/ } return; } sub isend { my $test = shift; foreach (@endings) { return 1 if $test =~ /$_/} return; }
As for your parsing loop, you can use the range operator in scalar context (flip-flop op):
foreach my $line (@lines) { push @extracted, $line if isbeg($line) .. isend($line); last if isend($line); }
You could drop the 'last' statement if the data might have more than one valid section you want to grab.
Also, a style note about your use of 'return undef' -- to return a generic false value just use 'return' with no arguments: it'll return undef in scalar context and an empty list in list context. Thus, not only it is shorter to type, it is more versatile as well.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re:(2) Parse Loops with flat text files. (code)
by deprecated (Priest) on May 13, 2001 at 23:51 UTC | |
by danger (Priest) on May 14, 2001 at 00:39 UTC | |
by Anonymous Monk on May 15, 2001 at 00:07 UTC |