htmanning has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

What am I doing wrong? This does not work:

if (($username eq "$security") && ($type ne "Check In" || $type ne + "Check Out")) {

But this does work:

if (($username eq "$security") && ($type ne "Check In")) {

I need to add several more arguments for $type. If the $username is correct and 3 or 4 $types are not true, I want to print something, but I can't get it to work. I'm missing something.

Replies are listed 'Best First'.
Re: And plus OR
by tybalt89 (Monsignor) on Jan 12, 2017 at 21:44 UTC
    $type ne "Check In" || $type ne "Check Out"

    $type is always going to be not equal to one or the other

Re: And plus OR
by AnomalousMonk (Archbishop) on Jan 12, 2017 at 21:40 UTC

    What does "does not work" mean?

    c:\@Work\Perl\monks>perl -wMstrict -le "my $username = 'phil'; my $security = 'phil'; my $type = 'foo'; ;; if (($username eq qq{$security}) && ($type ne 'Check In' || $type ne +'Check Out')) { print qq{\$username and \$security are '$username'}; print qq{\$type is '$type'}; } " $username and $security are 'phil' $type is 'foo'

    Update: Please see Short, Self Contained, Correct (Compilable), Example.

    Update 2: After looking at tybalt89's guess about your difficulty, I think it's likely right: you're expecting the expression
        $type ne "Check In" || $type ne "Check Out"
    to be false sometimes, and it can only be true.


    Give a man a fish:  <%-{-{-{-<

Re: And plus OR
by Marshall (Canon) on Jan 12, 2017 at 23:23 UTC
    Instead of the || (or), you need && (and) in your if statement. If I understand you correctly, you want to do the "if" as long as the $username is ok, except if the $type is either of 2 conditions. Edit: I guess in other words, you want to do the "if" if the $username is ok and $type="Something Else";.
    #!usr/bin/perl use strict; use warnings; my $security = "xxx"; my $username = "xxx"; my $type = "Check In"; if ($username eq "$security" and $type ne "Check In" and $type ne "Che +ck Out" ) { print "if executed, username=$username type=$type\n"; } else { print "if not executed, username=$username type=$type\n"; } __END__ if not executed, username=xxx type=Check In *** This is the same thing:*** if (($username eq "$security") and !( $type eq "Check In" or $type eq +"Check Out") )
    I show 2 different version of the "if", both do the same thing. This is called DeMorgan's theorem. Note that the "ne" becomes "eq" in the second version. I like the first version better, but your call... they are the same logically.
Re: And plus OR
by Anonymous Monk on Jan 12, 2017 at 22:09 UTC
    Search Wiki for De Morgan's Laws:
    if ($username eq $security && !($type eq "Check In" || $type eq "Check + Out")) { }
Re: And plus OR
by ablanke (Monsignor) on Jan 15, 2017 at 13:52 UTC
    for maintainability reasons the code could be changed to something like that:
    use List::Util qw(none); my @types = ( 'Check In', 'Check Out' ); if ($username eq $security && none { $type eq $_ } @types ){}