Doing a great big regex with lots of
/this|that|the other|etc/ is documented as probably being slower than
/this/ || /that/ || /the other/ || /etc/ (or at least was documented -- this may have been dropped when perlre.pod was created, perhaps because it is no longer true). Of course, this will vary by case so a benchmark will tell you for sure.
So, based on my old bias, I'd code it one of these two ways:
@problem = (
"0 OBS",
"AT LEAST",
"EXTRANEOUS",
"CARTESIAN",
"CLOSING",
"CONVERT",
"DIVISION BY ZERO",
"DOES NOT EXIST",
"DUE TO LOOPING",
"END OF MACRO",
"ENDING EXECUTION",
"ERROR",
"ERRORABEND",
"ERRORCHECK=STRICT",
"EXCEED",
"HANGING",
"HAS 0 OBSERVATIONS",
"ILLEGAL",
"INCOMPLETE",
"INVALID",
"LOST CARD",
"MATHEMAT",
"MERGE STATEMENT",
"MISSING",
"MULTIPLE",
"NOT FOUND",
"NOT RESOLVED",
"OBS=0",
"REFERENCE",
"REPEAT",
"SAS CAMPUS DRIVE",
"SAS SET OPTION OBS=0",
"SAS WENT",
"SHIFTED",
"STOP",
"TOO SMALL",
"UNBALANCED",
"UNCLOSED",
"UNINITIALIZED",
"UNREF",
"UNRESOLVED",
"WARNING"
);
# First way:
my $code= "sub { /("
. join ")/i || /(", map {"\Q$_\E"} @problem;
$code .= ")/i }";
my $match= eval $code;
die "$@" unless ref($sub)
&& UNIVERSAL::isa($sub,"CODE");
while(<>) {
if( &$match() ) {
print "line $.: problem: $1\n$_\n";
}
}
# Second way:
while(<>) {
my $up= upcase $_;
foreach my $p ( @problem ) {
if( 0 <= index($up,$p) ) {
print "line $.: problem: $p\n$_\n";
last;
}
}
}
Both of my solutions differ from the original in that they only report one problem per line. My "second way" can be made like the original by simply removing the "last;" line. I see no simple way to make my "first way" like the original.
I apologize, but I didn't feel up to creating a test input file so I didn't test nor benchmark. I'd be interested in seeing test and benchmark results on real data.
-
tye
(but my friends call me "Tye") |