> -----Original Message-----
> From: Charles Lu [SMTP:[EMAIL PROTECTED]]
> Sent: Wednesday, June 13, 2001 10:53 AM
> To:   [EMAIL PROTECTED]
> Subject:      Unexplainable behavior
> 
> The following snippet of code doesn't behave the way I want it to.  Yet i 
> cannot see why?
> 
> 
> $hash{s} = "T";
> 
> 
> if(exists($hash{s}) and $hash{s} ne "T" or $hash{s} ne "F") {
>       print "inside\n";
> }
> else{  print "outside\n"; }
> 
> 
This is a logic problem and can be explained by "De Morgan's Law".
Basically, if we analyze the if statement, it contains 3 parts.

exists($hash{s}) --> true
$hash{s} ne "T" --> false
$hash{s} ne "F" --> true

Hence, $hash{s} ne "T" or $hash{s} ne "F" --> true
And 'true' and 'true' --> true, so it goes 'inside'.

So you meant to only go inside if it does not equal
"T" or does not equal "F". This can be done by doing:

if(exists($hash{s}) and $hash{s} ne "T" and $hash{s} ne "F").

-Robin

Reply via email to