# (\$x == (\$y or \$z)) same as ((\$x==\$y) or (\$x==\$z)) ???

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

I expected (\$x == (\$y or \$z)) to return true if \$x equals either \$y or
\$z.  iow to give the same as ((\$x==\$y) or (\$x==\$z)).  But it doesn't.

Why?

## Re: (\$x == (\$y or \$z)) same as ((\$x==\$y) or (\$x==\$z)) ???

Because you told it (with parenthesis) to evaluate the "or"
before evaluating the "==".

And if you stop telling it that, ie:

(\$x == \$y or \$z)

Then it will evaluate the "\$x == \$y" followed by evaluating the "or"
(with one of its operands being the boolean result of the "\$x == \$y").

--
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

## Re: (\$x == (\$y or \$z)) same as ((\$x==\$y) or (\$x==\$z)) ???

Because even for Boolean values '==' and 'or' are not distributive, let
alone for non-Boolean values which you are probably using.

1 == (0 or 0)   ====>  1 == 0   ====> 0
(1 or 0) == (1 or 0)  ====> 1 == 1  ====> 1

jue

## Re: (\$x == (\$y or \$z)) same as ((\$x==\$y) or (\$x==\$z)) ???

He wants to distribute the == over the or, not vice versa, so that
would be:

(1 == 0) or (1 == 0)  ====> 0 or 0 ====> 0

works ;-).

Of course it doesn't work in the general case. E.g.,

0 == (0 or 1)         ====>  0 == 1  ====> 0
(0 == 0) or (0 == 1)  ====>  1 or 0  ====> 1

more specifically, since (\$y or \$z) returns \$y if \$y is not false, and
otherwise \$z, it works if \$x has a true value.

hp

## Re: (\$x == (\$y or \$z)) same as ((\$x==\$y) or (\$x==\$z)) ???

You can use effectively the same operations by installing
Quantum::Superpositions from CPAN, and it shouldn't break anything. See

http://search.cpan.org/~lembark/Quantum-Superpositions-2.02/lib/Quantum/Superpositions.pm

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl /

## Re: (\$x == (\$y or \$z)) same as ((\$x==\$y) or (\$x==\$z)) ???

Here is how I would do it in stock perl:

grep \$x == \$_, \$y, \$z

As in:

if ( grep { \$x == \$_ } \$y, \$z ) {
print 'x is the same as y or z';
}

Todd W.