Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
For your construction, I'd recommend a chained ternary expression:
print "$_ is ", $_ == 0 ? 'zero' : $_ > 0 ? 'pos' : 'neg', "\n" for -1..1;
The ternary operator ?: is essentially an if statement designed to return a value in a guaranteed-defined way. Breaking this down:
  • $_ == 0 ? 'zero' : ... does 'this is zero or something else'
  • $_ > 0 ? 'pos' : 'neg' does the 'something else': positive or negative
I've inserted the line breaks in this particular way to make it easy to see how the chain of logical expressions is evaluated. This also makes it easy to see where to edit new tests in:
print "$_ is ", $_ == 0 ? 'zero' : $_ == 2 ? 'two' : $_ == -1 ? 'minus one' : $_ > 0 ? 'pos' : 'neg', "\n" for -2..2;

In reply to Re: return value of "if" (documentation?) by pemungkah
in thread return value of "if" (documentation?) by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2023-06-10 22:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you go to conferences?






    Results (39 votes). Check out past polls.

    Notices?