<?xml version="1.0" encoding="windows-1252"?>
<node id="1005331" title="Replacing If Elsif Else with Hash" created="2012-11-23 23:59:32" updated="2012-11-23 23:59:32">
<type id="115">
perlquestion</type>
<author id="980508">
johnfl68</author>
<data>
<field name="doctext">
&lt;p&gt;Hello:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I had the following if elsif else:&lt;/p&gt;

&lt;code&gt;        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";
		}&lt;/code&gt;
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:

&lt;code&gt;    my %bkcolor = (
            "Extreme", "red",
            "Severe", "orange",
            "Moderate", "yellow",
            "Minor", "green",
            "", "grey"
        );

    my $background = $bkcolor{$severity};&lt;/code&gt;

&lt;p&gt;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?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Or is there another way I should be doing this?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks as always!&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;John&lt;/p&gt;</field>
</data>
</node>
