in reply to Data::Dumper / write output into Mysql

If you really just want to extract each MAC address from the hashref, this should do it:

use strict; use warnings; my $result = { '10.4.20.21' => { #IP Address from CPE equipment. '1' => '0x0a003edd8706', #Bridge table entry #1 '2' => '0x1a003edd8706', #Bridge table entry #2 '3' => '0xc03f0eddfb3b' #Bridge table entry #3 } }; for my $mac (values %{(values %$result)[0]}) { print "Mac addr: $mac\n"; }

Depending on your use case you may need more than this but hopefully it points you in the right direction.

Replies are listed 'Best First'.
Re^2: Data::Dumper / write output into Mysql
by Anonymous Monk on Oct 07, 2015 at 05:07 UTC
    Wow , thanks hippo. This is exactly what I needed :)
Re^2: Data::Dumper / write output into Mysql
by steph007 (Initiate) on Nov 05, 2015 at 09:50 UTC
    Hi hippo, The code you've helped me with works pretty well but if I understand the error correctly it seems like when the device isn't answering in time passing a value back to the script I'm getting the following error. Can't use an undefined value as a HASH reference at ./smwalker.pl line 152. ### This is basically Line 152
    for my $mac (values %{(values %$result)[0]}) { print "Mac addr: $mac\n"; }
    How do I deal with this ? Thanks in advance,

      On a modern enough perl you can use the defined-or operator // to use an empty hashref instead. eg:

      use strict; use warnings; my $result = { '10.4.20.21' => undef }; for my $mac (values %{(values %$result)[0] // {}}) { print "Mac addr: $mac\n"; }

      Of course, if your entire $result is undef then you could easily check for this separately before even hitting the for loop.

        I'm guessing I'll be doing something like this to check whether the entire $result is undef ?
        unless (defined($result)) { ....blah }