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.


In reply to Re: Parse Loops with flat text files. (code) by danger
in thread Parse Loops with flat text files. (code) by deprecated

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.