in reply to Parse Loops with flat text files. (code)

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