my $message = $status == 2 ? 'HIGH' : $status == 1 ? 'MODERATE' : 'LOW' print $message
Depending on the depth of status, you may want to use a hash
Or even a standard array if the values really are ints in a 0..n range.my %status_message = { 2 => 'HIGH', 1=>'MODERATE', 0=>'LOW' }; my $message = $status_message{ $status } || 'LOW';
These are both made a little more odd by the the inclusiveness of the default "LOW" value. Your demo code makes message "LOW" for any status other than 1 or 2.my @status_messages = qw( LOW MODERAGE HIGH ); my $message = $status_messages[$status] || 'LOW';
In reply to Re: Multiple if-else statements using C-style ternary operator
by spazm
in thread Multiple if-else statements using C-style ternary operator
by gansvv
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |