in reply to Re^2: Comparing multiple strings
in thread Comparing multiple strings

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; }

Replies are listed 'Best First'.
Re^4: Comparing multiple strings
by pryrt (Abbot) on Jan 15, 2019 at 22:42 UTC

    ++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