On Wed, 13 Jun 2001, Charles Lu wrote:

> 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 expression is true if either:

1. $hash{s} exists and it's not "T";

or

2. $hash{s} is not "F".

Therefore it's always true, because if it is "T" then it's not "F" and
(2) is true, and if it's "F" then it's not "T" and (1) is true.

The solution is to use the 'and' operator:

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

Then, the inside of the statement is executed only if ALL THREE
conditions are true.

- D

<[EMAIL PROTECTED]>

Reply via email to