Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

if statement confusion

by Anonymous Monk
on Jun 11, 2012 at 14:09 UTC ( #975572=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I was using the short c-style IF statment in my code and I understood it worked by EXPR ? TRUE :FALSE; however when I compiled this:

 6 < 8 ? $ans = "true" : $ans = "false";

$ans was false... can anyone explain what happened?

Replies are listed 'Best First'.
Re: if statement confusion
by toolic (Bishop) on Jun 11, 2012 at 14:15 UTC
Re: if statement confusion
by Anonymous Monk on Jun 11, 2012 at 14:13 UTC

    Operator precedence. What you got was: (6 < 8 ? $ans = "true" : $ans) = "false";

Re: if statement confusion
by davido (Cardinal) on Jun 11, 2012 at 17:31 UTC

    Write it like this:

    $ans = 6 < 8 ? 'true' : 'false';

    update: Aack! I didn't notice that toolic had already given this suggestion. Sorry for creating an echo in the Monastery. :)


    Dave

Re: if statement confusion
by Anonymous Monk on Jun 11, 2012 at 14:56 UTC

    Thank you :)

Re: if statement confusion
by eyepopslikeamosquito (Chancellor) on Jun 12, 2012 at 12:28 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://975572]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2019-12-13 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?