Something like this (untested):
#!/usr/bin/env perl
use strict;
use warnings;
my @files = <c:/perl64/myfiles/*>;
# record matching regexes
our %matched;
my @nums = ('1203', '1204', '1207');
my $regex = '\b(' . join('|', @nums) . ')\b'; # update: fixed + to .
for my $file ( @files ) {
open my $file_h, '<', $file or die "Can't open $file: $!";
while ( <$file_h> ) {
if (my ($match) = m/$regex/) {
$matched{$match} = 1;
print "$file $_";
}
}
}
# Check all nums have been seen
for my $num (@nums) {
if (not exists($matched{$num})) {
print "$num not found\n";
}
}
I would probably remove the @files and pass them on the command line (changing the for loop to while (<>)).
You might also do something more generic around the numbers, and allow those to be passed on the command line as well -- but this will take some command line parsing, or one of the GetOpt modules.
Update: Fixed capture problem, as noted by AnomalousMonk.
Update 2: Fixed concatenation issue.
-QM
--
Quantum Mechanics: The dreams stuff is made of
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|