IIRC , the original patterns are pulled from a database, but in the above case pulling the patterns from a file each time could perhaps be avoided by reading them all in at once:
open my $p, "< ". shift @ARGV
or die "could not open pattern file: $!";
my @patterns = <$p>;
chomp @patterns;
for my $filename (@ARGV) {
open my $f, "< $filename" or die "could not open $filename: $!";
my @lines = <$f>;
for my $pattern (@patterns) {
printf "%s has %d matches for pattern /%s/\n",
$filename,
scalar(grep /$pattern/, @lines),
$pattern;
}
}
Going back to the original pulling patterns from a db gives us:
# get column 0 from all result rows simultaneously....
my $results = $sth->fetchall_arrayref([0]);
# error check here?
# results are ref to array of hashes, so convert to array
# we could avoid this and use "results" directly
my @patterns = map { $_->[0] } @$results;
for my $filename (@ARGV) {
open my $f, "< $filename" or die "could not open $filename: $!";
my @lines = <$f>;
for my $pattern (@patterns) {
printf "%s has %d matches for pattern /%s/\n",
$filename,
scalar(grep /$pattern/, @lines),
$pattern;
}
}
A Monk aims to give answers to those who have none, and to learn from those who know more.
|