I'm very new to Perl, and currently I'm doing some basic exercises, one that is proposed is to insert a number and print out if it is positive or negative, without using if statements. I know how to do the logic, but I don't know how to print the output without using an if statement.
CodePudding user response:
Use the conditional operator.
For example:
perl -E '$n=42; $n==0 ? say "zero" : $n<0 ? say "negative" : say "positive"'
positive
perl -E '$n=-42; $n==0 ? say "zero" : $n<0 ? say "negative" : say "positive"'
negative
perl -E '$n=0; $n==0 ? say "zero" : $n<0 ? say "negative" : say "positive"'
zero
CodePudding user response:
You can try this print $number >= 0? "Positive":"Negative";