johnfl68 has asked for the wisdom of the Perl Monks concerning the following question:
Hello:
I had the following if elsif else:
It worked fine in the script as it was, but when I placed that script into a external subroutine, it broke things (took me a while to figure out that was the part of code causing the issue). So in reading around, many suggest using a hash instead of long if elsif sets. I understand that, and so now I have this:if ($severity eq "Extreme"){ $background = "red"; } elsif ($severity eq "Severe"){ $background = "orange"; } elsif ($severity eq "Moderate"){ $background = "yelow"; } elsif ($severity eq "Minor"){ $background = "green"; } else { $background = "grey"; }
my %bkcolor = ( "Extreme", "red", "Severe", "orange", "Moderate", "yellow", "Minor", "green", "", "grey" ); my $background = $bkcolor{$severity};
The question I have is, is there a way to handle the last 'else' when doing things in this manor, so that if 'severity' is anything other than the 4 known responses, it will always be grey?
Or is there another way I should be doing this?
Thanks as always!
John
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Replacing If Elsif Else with Hash
by frozenwithjoy (Priest) on Nov 24, 2012 at 05:07 UTC | |
by moritz (Cardinal) on Nov 24, 2012 at 08:59 UTC | |
by johnfl68 (Scribe) on Nov 24, 2012 at 05:29 UTC | |
Re: Replacing If Elsif Else with Hash
by AnomalousMonk (Archbishop) on Nov 24, 2012 at 07:59 UTC | |
by johnfl68 (Scribe) on Nov 24, 2012 at 08:51 UTC | |
by roboticus (Chancellor) on Nov 24, 2012 at 14:05 UTC | |
Re: Replacing If Elsif Else with Hash
by TomDLux (Vicar) on Nov 24, 2012 at 16:33 UTC | |
by sundialsvc4 (Abbot) on Nov 25, 2012 at 03:35 UTC |
Back to
Seekers of Perl Wisdom