next unless $description =~ /$regexp/i; $severity = $2;
If we reach this second line, the match must have succeeded, which would have reset $2 to undef.

A simpler way to do this is to use a hash for %severity_map. Heck, you already call it a map, but it's not in a normal form -- you have to unpack/decode it (with split) every time you use it to get to the actual data. That's a sure sign that you should rethink a data structure. With a hash, you can avoid having nested loops, and simply perform one regex match:

my %severity_map = ( "Error" => "Warning", "Critical" => "Critical", "System Shutdown" => "Fatal", "Failure" => "Critical", "Uncorrectable ECC" => "Fatal", "Warning" => "Minor", "Alarm" => "Critical", "System Powered Off" => "Fatal", "Memory Bank Deconfigured" => "Warning" ); my @description_samples = ( "Memory Bank Deconfigured", "Uncorrectable ECC", "Sticky Corrected ECC Error", "System Shutdown" ); my $keywords = join "|" => map quotemeta, keys %severity_map; for (@description_samples) { my $severity = "Warning"; /($keywords)/i ? $severity = $severity_map{$1} : print "Can't find severity mapping for: $_, using default\n" +; print "$_ has severity $severity\n"; }

blokhead


In reply to Re: obvious matching patterns don't match by blokhead
in thread obvious matching patterns don't match by Random_Walk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.