gansvv has asked for the wisdom of the Perl Monks concerning the following question:
How do I compose multiple if-else statements using the C-style ternary operator?
Say, $status can have the following values:
2 meaning "HIGH",
1 meaning "MODERATE", or
0 meaning "LOW".
How can I concisely code to print the message given the $status variable.
Here is my attempt, which prints the correct status, but is followed by a 1. What is this return value and why does it get printed?!
print eval { ($status == 2) ? print "HIGH " : (($status == 1) ? print "MODERATE " : print "LOW " ) };
With $status = 0, this prints: "LOW 1"
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Multiple if-else statements using C-style ternary operator
by Marshall (Canon) on Jul 11, 2011 at 22:44 UTC | |
Re: Multiple if-else statements using C-style ternary operator
by james2vegas (Chaplain) on Jul 11, 2011 at 22:41 UTC | |
Re: Multiple if-else statements using C-style ternary operator
by spazm (Monk) on Jul 11, 2011 at 22:46 UTC | |
by JavaFan (Canon) on Jul 11, 2011 at 23:04 UTC | |
Re: Multiple if-else statements using C-style ternary operator
by BrowserUk (Patriarch) on Jul 11, 2011 at 22:43 UTC | |
Re: Multiple if-else statements using C-style ternary operator
by gansvv (Initiate) on Jul 11, 2011 at 22:36 UTC |
Back to
Seekers of Perl Wisdom