in reply to obvious matching patterns don't match

Another problem is that your demo loop keeps overwriting $severity without giving you anything about successful matches. The commented print, if triggered, showed that you had a match.

I don't understand your need for a regex at all. You say that you get multiple matches with them, and so have to do without the tidy hash. I don't understand that.A hash key can only be found if it the exact string you try.

I notice that you are using case-insensitive matching. Was that the real reason, that some clients don't honor case of the error names? If so, you can change case in the descriptions before the hash lookup.

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", 'DANGEROUS DILITHIUM STATE', "Sticky Corrected ECC Error", "SYSTEM SHUTDOWN" ); for (@desctiption_samples) { my $severity = exists $severity_map{ lc($_) }? $severity_map{ lc($_) } : 'Warning'; print exists $severity_map{ lc($_) } ? "Severity of $_ is $severity_map{ lc($_) }\n" : "No map found for $_: '$severity' level assigned.\n"; }
Other than the case insensitivity, you're really testing for equality of strings. The regexen and the problems they cause are neither of them necessary.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: obvious matching patterns don't match
by Random_Walk (Prior) on Aug 18, 2004 at 17:07 UTC
    Hi Zaxo,

    Thanks for the reply. My code probably looked a bit odd in isolation, my test cases were very much cut down, I have about 500 various error texts to map to four Tivoli severities. Most of the error texts are simple as they contain the word Warning, Error or Critical some in mixed case, some all upper (this is why I use regex not an equality test) here are some typical examples

    WARNING CPU Temperature High
    disk read error
    System Powering Down
    Fan Powering Down
    
    The system powering down needs to be sending a more severe event than other generic powering downs so the order I compare the mappings when I characterise these error texts is important so no hash.

    I did not do anything with severity here as this is a fragment cut from a larger script. In reality the severity is stored in a structure with a bunch of other info characterising this alert that all goes into creating the Tivoli event (Sun's error identifier, Sub system info, The description itself etc).

    Cheers,
    Random.