My first impression is that your code is lacking in abstraction and encapsulation, but it's difficult to recommend specific changes based on this little snippet.

I would recommend reading the thread starting at SAS log scanner, which discusses using "|" concatenation to make more efficient regexes. And since you've already got the entire file snarfed into memory, you may as well operate on it as one large string buffer, instead of an array, which would eliminate all of the undesireable code. Though, if your files are large, I would suggest that you parse them as streams, as tilly has already recommended.

Your API / return value conventions really irk me, but we've gone over this before, and we agreed to disagree. I think it's worth mentioning, however, that other monks whose perl-fu is strong (hdp, danger) have taken you task on this very issue, and that I've seen you make several mistakes directly attributable to confusing return value logic. I have the feeling that this is a nasty habit carried over from a past of writing shell scripts. Well, when in Perl, do as the perlmonks do, or something like that...

Anyway, enough discussion :) here's a code sample which demonstrates some of the ideas I've suggested in this node:

my @beginnings = ( qr{This is valid</a>}, qr{[So]+(?:IS|isnt) this}, qr{start}, ); my @endings = ( qr{(?:we) Should not [be] [Pp]arsing after}, qr{either (?:o|f) these [Ll]ines}, qr{end}, ); my ($re_begin, $re_end, $re_extract); { local $" = '|'; $re_begin = qr{@beginnings}; $re_end = qr{@endings}; $re_extract = qr{$re_begin(.*?)$re_end}s; } # is_begin and is_end are no longer needed, but # you can see how trivial they've become after # concatenating the regexes with `|' sub is_begin { shift =~ $re_begin } sub is_end { shift =~ $re_end } # extract is equally simple sub extract { shift =~ $re_extract } my $text; { local $/; $text = <DATA>; } my ($extracted) = extract $text; print $extracted; __DATA__ blah blah blah start 4 5 6 end more blah blah
   MeowChow                                   
               s aamecha.s a..a\u$&owag.print

In reply to Re: Parse Loops with flat text files. (code) by MeowChow
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.