georgetovrea
georgetovrea

Reputation: 577

Logical operators or assignment in Perl cannot work?

I want to check my variable $id is equal 497 or equal 200 , if $id not equal 497 or $id not equal 200 then mail, but when i run the example as below, I set $id =497 by hand , but run the code ,it's print "not equal\n";

my $id = 497;
if($id != 497 || $id != 200)
   {
      print "not equal\n";
   }
else
  {
      print "equal , not to mail\n";
   }

Upvotes: 0

Views: 105

Answers (1)

ysth
ysth

Reputation: 98388

It says not equal because $id != 200 is true. || returns true if either operand is true.

You want to say:

if ($id != 497 && $id != 200)

so not equal is printed only when $id is neither 497 or 200.

Upvotes: 6

Related Questions