... a ... regex solution for this problem?
Since the application seems to call for exact string matching, I would prefer a hash lookup or any-based solution as discussed already in this thread.
But a regex solution might look like this:
c:\@Work\Perl\monks>perl -wMstrict -le
"my $t1 = 'EV-1891';
my $t2 = 'EV-DKL1';
;;
my ($rx_name_set) =
map qr{ \A (?: $_) \z }xms,
join '|',
map quotemeta,
reverse sort
$t1, $t2
;
print $rx_name_set;
;;
for my $name (@ARGV) {
if ($name =~ $rx_name_set) { print qq{name '$name' in set}; }
if ($name !~ $rx_name_set) { print qq{name '$name' NOT in set}; }
}
" EV-1891 FOO
(?msx-i: \A (?: EV\-DKL1|EV\-1891) \z )
name 'EV-1891' in set
name 'FOO' NOT in set
Since the regex is
\A \z anchored for an exact match, the
reverse sort
step isn't really necessary, but it can't hurt. See
Building Regex Alternations Dynamically. As to how
fast it is, I leave that to your
Benchmark-ing. My guess is that this approach should be fairly fast if you don't have to build the regex anew each time you do a set of comparisons; if you do, forget it.
Give a man a fish: <%-{-{-{-<
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.