in reply to obvious matching patterns don't match

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

Replies are listed 'Best First'.
Re^2: obvious matching patterns don't match
by Random_Walk (Prior) on Aug 18, 2004 at 16:01 UTC
    I started using a hash but then found some errors can match multiple patterns so I needed to keep the map in order to be sure what would match where. That was the hack (ugly replacement of hash with munged array) that got me into this problem. The code that worked fine with the hash now got clobbered by some dodgy use of regexp.

    thanks for the suggestion anyway.