Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

&& and ne

by malaga (Pilgrim)
on Feb 08, 2001 at 22:19 UTC ( [id://57223]=perlquestion: print w/replies, xml ) Need Help??

malaga has asked for the wisdom of the Perl Monks concerning the following question:

does anyone know why i can't add another:

&& $myrowidarray[$myint] ne "foo")


to this statement?:

if ($myrowidarray[$myint] ne "del" && $myrowidarray[$myint] ne "cal")

I've tried using || and i've looked for answers, but it seems like it should work to do this:

if ($myrowidarray[$myint] ne "del" && $myrowidarray[$myint] ne "cal" && $myrowidarray[$myint] ne "foo")

but it doesn't. thanks

Replies are listed 'Best First'.
(tye)Re: && and ne
by tye (Sage) on Feb 08, 2001 at 22:34 UTC

    Cut'n'paste the actual code that "doesn't work" and cut'n'paste the actual error message. Just a guess, but could it be that you actual code looks like this:

    if($myrowidarray[$myint] ne "del" && $myrowidarray[$myint] ne "cal") && $myrowidarray[$myint] ne "foo")

    which has an extra ")". (But that is just a guess.)

            - tye (but my friends call me "Tye")
Re: && and ne
by Pahrohfit (Sexton) on Feb 09, 2001 at 02:38 UTC
    Instead of all the && and ne's, try using some regexs like so:
    if ($myrowidarray[$myint] !~ /(del|cal|foo)/) { }

      That's a nice thought. Just to add, if you absolutely must test for equality, put in the relevant anchors:

      if ($myrowidarray[$myint] !~ /^(del|cal|foo)$/) {}

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: && and ne
by kilinrax (Deacon) on Feb 08, 2001 at 22:25 UTC
    Try using brackets to define the way you want that statement evaluated more precisely:
    if (($myrowidarray[$myint] ne "del") && ($myrowidarray[$myint] ne "cal +"))
    Update: well it seems that 'ne' has a higher precedence than '&&', so the above code should work. I tend to use brackets out of habit anyway, as they make the code easier to read.
Re: && and ne
by Fastolfe (Vicar) on Feb 08, 2001 at 22:21 UTC
    Define "doesn't work".

    Your code is syntactically valid (assuming you have a valid block of code following it) and there are no precedence issues. We need more information. Perhaps include one or two lines of code in either direction so we can see if something else might be causing your problem. Please post the error messages you are seeing.

Re: && and ne
by sierrathedog04 (Hermit) on Feb 08, 2001 at 22:39 UTC
    You can. The following runs on my ActiveState Perl 5.6 system:
    use strict; my @myrowidarray; $myrowidarray[0] = 100; my $myint = 0; if ($myrowidarray[$myint] ne "del" && $myrowidarray[$myint] ne "cal" & +& $myrowidarray[$myint] ne "foo") { print $myrowidarray[$myint]; }
    I think the problem might be that you have a right parenthesis after your initial set of conditions, and then your additional condition has yet another right parenthesis.
Re: && and ne
by malaga (Pilgrim) on Feb 08, 2001 at 22:47 UTC
    in trying to define this further, i'm getting the idea that the culprit is in a different script - that this one is calling. or one of a few. it will take some digging. i think the code i submitted above does actually work. i might be back. :) thanks. update: yes, the culprit was a different script. there's a "web" of scripts here, and sometimes there aren't many clues. sorry for false alarm, but i appreciate the answers - i still learned from them.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://57223]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-29 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found