Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Comparing multiple strings

by rsFalse (Chaplain)
on Jan 16, 2019 at 11:17 UTC ( [id://1228646]=note: print w/replies, xml ) Need Help??


in reply to Comparing multiple strings

if ($name ne $t1 || $t2) { do something }
Note that conditional will always be true if $t2 will be true, because '||' operator is lower in precedence than 'ne' (precedence list -- perlcheat). I guess OP asks maybe for 'shorter' (faster to write/maintain) way than $name ne $t1 || $name ne $t2.
And is there a fast regex solution for this problem?

Replies are listed 'Best First'.
Re^2: Comparing multiple strings
by AnomalousMonk (Archbishop) on Jan 16, 2019 at 22:11 UTC
    ... 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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1228646]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-25 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found