Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Pushing an hash into an array

by sami.strat (Novice)
on May 14, 2013 at 03:13 UTC ( #1033390=perlquestion: print w/replies, xml ) Need Help??
sami.strat has asked for the wisdom of the Perl Monks concerning the following question:

This is one of my first perl apps and I'm having issues pushing a hash into an array.

The code sample is below, I've documented the place where the problem lies. Any help would be greatly appreciated. Thanks in advance.

#!/usr/bin/perl my $RESULT="OK"; use strict; use warnings; my $STATE_OK=0; my $STATE_WARNING=1; my $STATE_CRITICAL=2; my $STATE_UNKNOWN=3; my %error_code = ( SysBatteryFailure => 'System Battery Failure', PowerRedundencyFailure => 'Power Redundency Failure', FltTolPowerSupplyFailed => 'Fault Tolerant Power Suppl +y Failure', ); my $SysBatteryFailure=`echo \"Battery Failure\" | grep Failure`; my $PowerRedundencyFailure=`echo \"Power Redundency Failure\" | grep t +rash`; my $FltTolPowerSupplyFailed=`echo \"Power Supply Failure\" | grep tras +h`; my @STATUS = ($SysBatteryFailure, $PowerRedundencyFailure, $FltTolPowe +rSupplyFailed); my @ILO_ERRORS = (); foreach my $element (@STATUS) { chomp $element; if ($element) { # The element variable is accurate here print "$element\n"; # the following line fails. I want to push the hash # into the array. Any idea why it fails? push (@ILO_ERRORS, $element); } } if (@ILO_ERRORS) { print "SNMP CRITICAL - ", @ILO_ERRORS; exit $STATE_CRITICAL; } else { print "SNMP OK - $STATE_OK"; exit $STATE_OK; }

The result of this code is:

root@yosemite SNMP# ./2

Battery Failure

Use of uninitialized value $ILO_ERRORS1 in print at ./2 line 37.


root@yosemite SNMP# vi 2

Replies are listed 'Best First'.
Re: Pushing an hash into an array
by NetWallah (Canon) on May 14, 2013 at 03:28 UTC
    Welcome to the Monastery!

    $element contains "Battery Failure".

    There is no such key in %error_code, so $error_code{$element} returns undef.

    The nearest valid key would be "SysBatteryFailure" , so you need to figure out how to get that into $element.

    ALso, you need to decide if @ILO_ERRORS has a hashref element, or is a simple array. You are attempting to use it both ways.

    After you get $element set correctly, if you want to use @ILO_ERRORS as an array, try:

    my @ILO_ERRORS = (); # Empty LIST; not {}, which is a hash ref ... push @ILO_ERRORS, $error_code{$element}; .... if (@ILO_ERRORS) { print "SNMP CRITICAL - @ILO_ERRORS \n"; # Note - array interpolated + INSIDE QUOTES, "\n" added ...
    To get $element to work right, you could try:
    my ($key) = grep {$error_code{$_} =~/$element/} keys %error_code; # Note - parens around ($key) provide the required list context $key and push @ILO_ERRORS, $error_code{$key};

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

      Thanks much. Your recommendations were actually very helpful.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1033390]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2018-05-27 23:46 GMT
Find Nodes?
    Voting Booth?