I agree with your reading of the post (I wonder if biggin777 does too...), and with your approach (well, erm, if you're scanning through a directory, then it seems a bit harsh to die because open fails on a given file).

Anyway, since the files are big, it would be nice to exit the while loop ASAP. Granting that all three conditions need to be met to trigger further processing, there's no point in keeping track of them separately:

my @keepers; opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!"; while ($file = readdir(<DIR>)) { if ($file =~ /Somthing/ and -f $file and open(FILE, $file) { my $pass = 0; while (<FILE>) { $pass++ if (/\b(?:PASS|sweeps|Final)\b/); last if ( $pass == 3 ); } close FILE; push @keepers, $file if ( $pass == 3 ); } } # now do whatever needs to be done with @keepers.
(or maybe something needs to be done with @keepers in that same while loop? but that might complicate things a lot; perhaps there'll be another question from biggin777 about that in a little while...)

update: Thanks to AM's very astute reply below, I see where it might be important to keep track of each different condition separately. To keep it brief, I would just set a different bit for each condition:

... my $pass = 0; while (<FILE>) { $pass |= 1 if ( /\bPASS\b/ ); $pass |= 2 if ( /\bsweep\b/ ); $pass |= 4 if ( /\bFinal\b/ ); if ( $pass == 7 ) { push @keepers, $file; last; } } ...

In reply to Re: Re: Perl Matching Question by graff
in thread Perl Matching Question by biggin777

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.