in reply to Perl Matching Question
BrowserUK's solution implies that you want to do something with a file if it contains one of the words 'PASS', 'sweeps', 'Final', but from your post, I take it a file should match all three of them. The following code snippet should do that.
This is not going to win a prize for elegance or generality, but it should be along the lines you want.open(FILE, "$some_dirr/$file") or die("can't open $file"); my ($PASSmatch, $sweepsmatch, $Finalmatch); while (<FILE>) { if (/\bPASS\b/) { $PASSmatch = 1; } elsif (/\bsweeps\b/) { $sweepsmatch = 1; } elsif (/\bFinal\b) { $Finalmatch = 1; } } close(FILE); if ($PASSmatch && $sweepsmatch && $Finalmatch) { # do whatever with this file. }
Hope this helps, -gjb-
Update: graff makes two good points about the code above: 1) one shouldn't die in a directory scan when a file can't be read and 2) it would be more efficient to last out of the while as soon as the three words have been found for efficiency's sake.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Perl Matching Question
by graff (Chancellor) on Sep 11, 2003 at 06:19 UTC | |
by Anonymous Monk on Sep 11, 2003 at 06:46 UTC |