Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

and/or foreach loop- i'm overlookin somethin

Status
Not open for further replies.

Zas

Programmer
Oct 4, 2002
211
0
0
US
Now heres my problem. In the following coding, it also comes up false ( it tells the user to go back ), and it shouldn't, I believe. $itemeq always ends up the number 1. It basicly says if $pid (the querry), is equal to the the var in the forearch loop, than return false. But $pid is 2, and $itemeq is 1. It also says OR if $pid equals one AND $itemeq equals 9, or 10, than also return false. But again $pid equals 2, and $itemeq equals 1, so what is wrong with my and/ors?

foreach $itemeq (@EITEM) {
print "$itemeq,$pid";
if (($pid eq $itemeq) ^ (($pid eq "1") && ($itemeq eq "9" ^ "10"))) { print "Already something equiped around your
<b>Neck</b>. Go <a href=\&quot;javascript: history.go(-1)\&quot; target=\&quot;_self\&quot;>back</a>.&quot;; exit; }
if (($pid eq $itemeq) ^ (($pid eq &quot;2&quot;) && ($itemeq eq &quot;9&quot; ^ &quot;10&quot;))) { print &quot;Already something equiped around your
<b>Wrists</b>. Go <a href=\&quot;javascript: history.go(-1)\&quot; target=\&quot;_self\&quot;>back</a>.&quot;; exit; }
if (($pid eq $itemeq) ^ (($pid eq &quot;3&quot;) && ($itemeq eq &quot;9&quot; ^ &quot;10&quot;))) { print &quot;Already something equiped in your
<b>Pockets</b>. Go <a href=\&quot;javascript: history.go(-1)\&quot; target=\&quot;_self\&quot;>back</a>.&quot;; exit; }
if ((($pid eq &quot;4&quot;) && ($itemeq eq &quot;9&quot; ^ &quot;10&quot;)) ^ ($pid eq $itemeq)) { print &quot;Already something equiped in your
<b>Pouch</b>. Go <a href=\&quot;javascript: history.go(-1)\&quot; target=\&quot;_self\&quot;>back</a>.&quot;; exit; }
} Happieness is like peeing your pants.
Everyone can see it, but only you can feel the warmth.
 
Zas,

I can't read that, have another go with formatting and without the web stuff please.

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Your basic mistake is using ^ for OR. The ^ is a bitwise exclusive-OR operation; use || for a logical OR. Also,
the expression ($itemeq eq &quot;9&quot; ^ &quot;10&quot;) won't do what you want either - even after changing the ^ to ||. Change your if statements to something like this:
if (($pid eq $itemeq) || (($pid eq &quot;1&quot;) && ($itemeq eq &quot;9&quot; || $itemeq eq &quot;10)) { print ... }

 
BTW, if $pid eq 1 and that negates caring what $itemeq is, then the above won't work either, as it will still matter what $itemeq is. If you're doing what I think you're doing, then what you want might be something like this:
Code:
 if (($pid eq &quot;1&quot;) || (($pid eq $itemeq) && ($itemeq =~ /^(?:9|10)$/))) { print ... }
But I could be wrong about the logic of what you're ultimately doing here.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Ok, I'll explain. It was for equipment. If its being used, than I cannot wear another one. If it shares a type with another one (9 and 10 share the type), than that won't work either. The problem was && was supposed to be &. Thats strange.
So heres a new question, whats the difference between &&, &, ^^, ^, ||, and |. Happieness is like peeing your pants.
Everyone can see it, but only you can feel the warmth.
 
The difference is that the double-symbols (&&,||) are logical operators, whereas the single-symbols (&,|,^) are bitwise operators. Bitwise operators only work on binary numbers, thus other variable types are probably converted before comparisons. Logical operators work on strings and numbers. Anything which is zero, undef, or blank is false, and anything else is true.

I don't think that ^^ is a valid perl operator. The single ^ is a bitwise xor. For a logical xor, you must use the word &quot;xor&quot;. Also, for logical operators, there are word equivalents to the symbols which are a lower precedence than the symbol versions. For example,
Code:
not $a && $b
means
Code:
!($a && $b)
, whereas
Code:
! $a && $b
means
Code:
(not $a) && $b
, and
Code:
! $a and $b
means
Code:
!$a && !$b
.

Check out for the full list of perl operators and their precedence.

Does that answer the question?
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Oops, after a second look, that last one is wrong...

Code:
! $a && $b
and
Code:
! $a and $b
mean exactly the same thing, because in both cases the ! operator has higher precedence than either
Code:
&&
or
Code:
and
... they both mean the same as
Code:
(not $a) && $b
.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top