in reply to Re: Perl Matching Question
in thread Perl Matching Question

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; } } ...

Replies are listed 'Best First'.
Re: Re: Re: Perl Matching Question
by Anonymous Monk on Sep 11, 2003 at 06:46 UTC

    With this code if you put PASS sweeps FINAL on the same line, it fails. Also, you do not designate which words you've seen. As such, the file could say PASS on each line and the program will accept it. Too bad the files are so big as you could otherwise have one line inside your while ($file ... loop:

    push @keepers, $file if $file=~/Something/ and -f $file and 3 == @{ [ +do { my @a= (my $temp = do{local(*ARGV,$/)=[$file];<>}) =~ /\b(PASS|s +weeps|Final)\b/g;my %b;undef @b{@a};keys %b } ] };

    Yeah, no error checking or anything. :)

    Anonymously yours
    Anonymous Monk