Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Trouble getting data out of a hash

by wruehl (Acolyte)
on Oct 26, 2007 at 12:50 UTC ( #647371=perlquestion: print w/replies, xml ) Need Help??

wruehl has asked for the wisdom of the Perl Monks concerning the following question:

For some reason Perl is giving me and unitialized variable error when attempting to take a value out of a hash. Is there any other way of getting the data out of the hash? That same code worked before(gathering port data from a switch) so I'm wondering why it isn't working this time.

The exact output for one xml element is as follows:

Pairname is: RP261

value is HASH(0x10b0060)

Use of uninitialized value in concatenation (.) or string at line 56.

StatA is

#!/usr/bin/perl -w #Status Legend : 1=psNotInstalled,2=psAvailable,3=psBlocked,4=psUnavai +lable, #5=psLinkFailure,6=psLinkFailLOL,7=psIntDiags, 8=psExtLoop,9=psPortFai +l, #10=psSR,11=psLR,12=psInaccessible,13=psInactive ### This script developed by Bill Ruehl, July 2007 ### Version 1.0 This is version 1 of the time check script use strict; #modules used use XML::Simple; use Net::SNMP; use XML::Parser; use XML::Parser::Expat; our ($data, $xml, $e, $value, $IPAddressA, $OID, $session, $error, $st +atA,$resmsg); #create object # Hard Code OID for port status info $OID ="" ; #Hard Code OID for metrics info #instantiate a parser $xml = new XML::Simple; open LOG, ">>/home/pbkhk/test/log.txt"; # read the XML file $data = $xml->XMLin("/home/pbkhk/test/timetest.xml"); print LOG "This iteration run at: " . localtime() . "\n" ; foreach $e (@{$data->{PairedDevices}}) #Do actions that require use of XML data in this loop possibly #call SNMP data gathering and reporting { print "Pairname is: " . $e->{PairName} . "\n "; print LOG "Pairname is: ". $e->{PairName} . "\n "; $IPAddressA = $e->{DeviceA}->{IPAddress} ; #print "IpAddressA is " . $IPAddressA . "\n"; #print "IpAddressB is " . $IPAddressB . "\n"; if ($e->{ExclusionIndicator} ne "y") #Check to see if host sh +ould be ignored { #print "SNMP test for DeviceA \n"; ($session,$error) = Net::SNMP->session(Hostname => $IPAddr +essA, Community => "public"); } $value = $session->get_request($OID); print LOG "request error " . $session->error unless (defin +ed $value); $session->close; $statA = $value->{$OID}; print "value is $value \n" ; print "StatA is $statA \n"; #print "Date and Time for : $e->{PairName} was $statA \n" +; #print LOG "Date and Time for : $e->{PairName} was $statA \n +"; } close LOG;


Replies are listed 'Best First'.
Re: Trouble getting data out of a hash
by holli (Abbot) on Oct 26, 2007 at 12:58 UTC
    If your code worked before and you didn't change anything, the problem is most likely with your data. try
    use Data::Dumper; print Dumper($value);
    and see if the data is correct.

    btw, your code is correct (assuming $value is really a hashref).

    holli, /regexed monk/
Re: Trouble getting data out of a hash
by toolic (Bishop) on Oct 26, 2007 at 13:03 UTC
    Use of uninitialized value in concatenation (.) or string at line 56.
    You could use Data::Dumper to help narrow down the cause of this warning. At the top of your code, add:
    use diagnostics; use Data::Dumper;
    Later on in your code, use:
    print Dumper($value);
    diagnostics might also provide more helpful information.
      Using Data::Dumper gives me the following:

      value is HASH(0xe8a5b0)

      Pairname is: PD110

      $VAR1 = {};

      It looks like there might be a problem with the stored hash. Any suggestions?

        First of all, the returned hash is empty so it's no wonder that you the uninitialzed warning. I've never used Net::SNMP but from a quick glance at the docs I see:
        $result = $session->get_request( [-callback => sub {},] # non-bloc +king [-delay => $seconds,] # non-bloc +king [-contextengineid => $engine_id,] # v3 [-contextname => $name,] # v3 -varbindlist => \@oids, );
        so I think you're call of request is wrong and should read
        $result = $session->get_request( -varbindlist => [$OID] );

        holli, /regexed monk/

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://647371]
Approved by holli
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-08-18 23:09 GMT
Find Nodes?
    Voting Booth?

    No recent polls found