in reply to Comparing multiple strings

Here's a few ways...

If you're using a version of Perl that has smart match:

if ($name ~~ [$t1, $t2]) { ...; }

I've written match::smart which has a pure Perl implementation of smart match, which will allow you to do smart matching on older Perls:

use match::smart "match"; if (match $name, [$t1, $t2]) { ...; }

A lot of people don't like smart match though because the rules for exactly how it behaves are kind of confusing. match::smart comes bundled with match::simple which has saner rules and will also do what you're looking for:

use match::simple "match"; if (match $name, [$t1, $t2]) { ...; }

You can use List::Util's any function:

use List::Util "any"; if (any { $name eq $_ } $t1, $t2) { ...; }

match::simple has an optional XS implementation, match::simple::XS, and if you can install it, that will probably be faster than List::Util or match::smart.

Replies are listed 'Best First'.
Re^2: Comparing multiple strings
by bigup401 (Pilgrim) on Jan 15, 2019 at 22:17 UTC

    i have tried list util. but dont know the problem

    THIS DONT WORK $my1 = # IS "EV-1891" OR "EV-DKL1" thats wat we want; #THEN AM TRYING TO COMPARE IF NOT $my1 results if (any { $my1 ne $_ } 'EV-1891', 'EV-DKL1') { print "BAD"; last; } # MAKE OTHER NEXT CONDITION ACCESSIBLE IT ONLY WORKS WHEN I REMOVE 1 STRING THIS WORKS if (any { $my1 ne $_ } 'EV-1891') { print "BAD"; last; } # NEXT CONDITION ACCESSIBLE

    and my problem i want to compare two things ( if we dont get EV-DKL1 or EV-1891) show error

      Have you tried converting your attempts to plain English, and reading them to yourself? That may help you.

      if (any { $my1 ne $_ } 'EV-1891', 'EV-DKL1') { print "BAD"; last; }
      becomes "if any of a list of strings are not equal to my special string, then call the whole thing bad". When I say that out loud, it seems reasonable that your if would return true, and you would print "BAD", every time: your list of strings are not all the same string; so even if one is equal to $my1, the others aren't -- so "any" of them are "not equal to $my1", which means it will always print "BAD", unless your whole list are the same string, and match $my1.

      What you really seem to want is "call it bad unless there's at least one that does match". In that case, the logic could be: unless( any { $my1 eq $_ } 'EV-1891', 'EV-DKL1') { print "BAD"; last; }, or TIMTOWTDI, if( none { $my1 eq $_ } 'EV-1891', 'EV-DKL1') { print "BAD"; last; }

        ++hippo for the third TIMTOWTDI, which translates to if( all { $my1 ne $_ } 'EV-1891', 'EV-DKL1') { print "BAD"; last; }. Of these three, I would probably select unless( any { $my1 eq $_ } 'EV-1891', 'EV-DKL1') { print "BAD"; last; }, because the any can short-circuit faster; if the first one matches, it won't have to test all the others in the list: for the example list of two, that's irrelevant, but if there are dozens or hundreds or thousands to compare against, any would be faster than all ... ne or none ... eq

      and my problem i want to compare two things ( if we dont get EV-DKL1 or EV-1891) show error

      So you are saying you want all instead of any.

Re^2: Comparing multiple strings
by bigup401 (Pilgrim) on Jan 15, 2019 at 19:09 UTC

    thanks