I am writing a script that takes alerts from Sun hardware and maps part of the error message to an IBM Tivoli error severity (for eventual diplay on Tivoli system management).

Strangely it does not match strings that look to me like obvious matches, it worked when I used a hash to store the mappings (key was pattern, value was mapped severity) but had to change this as processing order is important

I know I must be missing something obvious but I have gone snow blind from staring at it

The following is a reduced test case showing the problem.

#!/usr/bin/perl -w use strict; ################### Test arrays ######################## my @severity_map = ( "Error:Warning", "Warning:Minor", "Critical:Critical", "Alarm:Critical", "System Shutdown:Fatal", "System Powered Off:Fatal", "Failure:Critical", "Memory Bank Deconfigured:Warning", "Uncorrectable ECC:Fatal" ); my @description_samples = ( "Memory Bank Deconfigured", "Uncorrectable ECC", "Sticky Corrected ECC Error", "System Shutdown" ); ################### The code ######################### foreach my $description (@description_samples) { my $severity; foreach (@severity_map) { /(.*):(.*)/; my $regexp = $1; # uncomment following to see why this drives me mad # print "does $description=~/$regexp/i\n"; next unless $description =~ /$regexp/i; $severity = $2; } unless ($severity) { print "can find no severity mapping for: $description "; print "defaulting to WARNING\n"; $severity = "Warning"; } }

Please lead me on the path to enlightenment


And verily did the regexp smite the $2(ites)
Many thanks to all for the quick responses, I was looking upstream of my match and completely forgetting the $2 downstream, doh ! Also as beernuts pointed out I should have been using split /:/ in place of the regexp.
I have now re-written it with an array of anon arrays for the mapping, this makes it a lot neater, should have done it that way in the first place.
my @severity_map = ( ["Error","Warning"], ["Warning","Minor"], ["Critical","Critical"], ["Alarm","Critical"], . . . foreach (@severity_map) { next unless $description =~ /$_->[0]/i; $severity = $_->[1]; }

In reply to 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.