Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Help with this statement

by basicdez (Pilgrim)
on Jan 11, 2002 at 19:54 UTC ( #138034=perlquestion: print w/replies, xml ) Need Help??

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

why does this not work?
my value = 444; if ($value == 456 || 453 ) { print something; } else { print nothing; }
Why is this printing something rather than nothing.

peace, LOVE and ((code))

basicdez

Replies are listed 'Best First'.
Re: Help with this statement
by jwest (Friar) on Jan 11, 2002 at 19:59 UTC
    Your if clause looks to perl like:
    if (($value == 456) || (453))
    instead of what you intend.

    --jwest

    -><- -><- -><- -><- -><-
    All things are Perfect
        To every last Flaw
        And bound in accord
             With Eris's Law
     - HBT; The Book of Advice, 1:7
    
Re: Help with this statement
by talexb (Chancellor) on Jan 11, 2002 at 20:29 UTC
    The statement
    if ($value == 456 || 453 )
    works out as follows: "If variable value equals 456 OR if 453 is non-zero ...". Of course, 453 will always be non-zero. :)

    What you probably want to say is

    if ($value == 456 || $value == 453)

    --t. alex

    "Excellent. Release the hounds." -- Monty Burns.

Re: Help with this statement
by tradez (Pilgrim) on Jan 11, 2002 at 20:48 UTC
    I believe your problem is the non-transative nature of the perl || and 'or' operators. in this case you are asking if it is equal to 456 or just 453. 453 of course is never 0 and therefore will be true. Hence printing something. to fix this just add.
    if ($value == 456 || $value == 453){ print something; } else { print nothing; }
    That should clear things up.
      I couldn't resist this as a TIMTOWTDI :)
      unless (abs($value - 454.5) - 1.5) { print something; } else { print nothing; }

      cLive ;-)

        just as an alternative, it can also be written as:
        my $value = 444; if ($value == 456) { print "something"; } elsif ($value == 453) { print "something"; } else { print "nothing"; }
      Thanks I got it working now. peace dez L
Re: Help with this statement
by BazB (Priest) on Jan 11, 2002 at 20:02 UTC
(crazyinsomniac) Re: Help with this statement
by crazyinsomniac (Prior) on Jan 12, 2002 at 09:01 UTC
    If you can't memorize perlop and the precedence, and I tell you I cannot, the way to figure out how "perl" is reading yer program, is to use O::Deparse, so that's what I did (although this example was simple enough to figure out):
    bash-2.05$ perl -e'$v = 444;if($v == 456 || 453){print 1}else{print 2} +' 1bash-2.05$ bash-2.05$ bash-2.05$ bash-2.05$ perl -MO=Deparse -e'$v = 444;if($v == 456 || 453){print 1}e +lse{print 2}' -e syntax OK $v = 444; if ($v == 456 or 453) { print 1; } else { print 2; }
    Now that's not what you thought would happen. Perl is pretty cool, and there is much voodoo within, but the *binary* operators, cannot be chained as such . The term binary implies that they operate on 2 values, the one on the left, and the one on the right. it's not the one on the left, and the 2 on the right (usually). ++ and -- unary operators. they operate on 1 value. now you know. peace

     
    ______crazyinsomniac_____________________________
    Of all the things I've lost, I miss my mind the most.
    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Re: Help with this statement
by screamingeagle (Curate) on Jan 12, 2002 at 05:22 UTC
    Another way of doing this is simulate a switch-case construct.Here's an example from the Perl documentation on how to do this :
    SWITCH: for (ref $whatchamacallit) { /^$/ && die "not a reference"; /SCALAR/ && do { print_scalar($$ref); last SWITCH; }; /ARRAY/ && do { print_array(@$ref); last SWITCH; }; /HASH/ && do { print_hash(%$ref); last SWITCH; }; /CODE/ && do { warn "can't print function ref"; last SWITCH; }; # DEFAULT warn "User defined type skipped"; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-01-24 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (64 votes). Check out past polls.

    Notices?