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

First time poster and new to hash of hashes. I am trying to learn how to navigate around hash of hashes. I have used XML::Simple::XMLin to import a NetCONF (XML) file to $xml. I am trying to print specific parts of the hash, but not with much success using:

print $xml->{'bgp-information'}->{'bgp-peer'}{[0]}->{'peer-address'};

I am also trying to figure out how I can while loop all the "peer-address" entries (e.g. 1.1.1.1, 2.2.2.2, 3.3.3.3) to print entries within. Any input would be greatly appreciated.

Thanks,
~Ethan



$VAR1 = { 'xmlns' => 'urn:ietf:params:xml:ns:netconf:base:1.0', 'bgp-information' => { 'group-count' => '3', 'xmlns' => 'http://xml.juniper.net/juno +s/8.0R3/junos-routing', 'bgp-rib' => { 'total-external-prefix-cou +nt' => '222224', 'damped-prefix-count' => ' +0', 'active-prefix-count' => ' +223534', 'pending-prefix-count' => +'0', 'suppressed-internal-prefi +x-count' => '0', 'active-internal-prefix-co +unt' => '16579', 'name' => 'inet.0', 'junos:style' => 'brief', 'total-internal-prefix-cou +nt' => '69811', 'suppressed-external-prefi +x-count' => '0', 'history-prefix-count' => +'0', 'received-prefix-count' => + '292035', 'active-external-prefix-co +unt' => '206955', 'suppressed-prefix-count' +=> '0', 'bgp-rib-state' => 'BGP re +start is complete', 'total-prefix-count' => '2 +92035' }, 'peer-count' => '3', 'bgp-peer' => [ { 'peer-address' => '1.1. +1.1', 'bgp-rib' => { 'received- +prefix-count' => '69811', 'active-pr +efix-count' => '16579', 'suppresse +d-prefix-count' => '0', 'name' => +'inet.0' }, 'peer-state' => { 'junos: +format' => '16579/69811/0 0/0/0', 'conten +t' => 'Established' }, 'peer-as' => '1000', 'junos:style' => 'terse +', 'heading' => 'Peer + AS InPkt OutPkt OutQ Flaps Last Up/Dwn State| +#Active/Received/Damped...', 'elapsed-time' => { 'juno +s:seconds' => '8579868', 'cont +ent' => '14w1d7h' }, 'route-queue-count' => +'0', 'flap-count' => '0', 'input-messages' => '41 +69581', 'output-messages' => '8 +472027' }, { 'peer-address' => '2.2. +2.2', 'bgp-rib' => { 'received- +prefix-count' => '1', 'active-pr +efix-count' => '1', 'suppresse +d-prefix-count' => '0', 'name' => +'inet.0' }, 'peer-state' => { 'junos: +format' => '1/1/0 0/0/0', 'conten +t' => 'Established' }, 'peer-as' => '2000', 'junos:style' => 'terse +', 'elapsed-time' => { 'juno +s:seconds' => '8580144', 'cont +ent' => '14w1d7h' }, 'route-queue-count' => +'0', 'flap-count' => '0', 'input-messages' => '28 +6000', 'output-messages' => '2 +85903' }, { 'peer-address' => '3.3. +3.3', 'bgp-rib' => { 'received- +prefix-count' => '222223', 'active-pr +efix-count' => '206954', 'suppresse +d-prefix-count' => '0', 'name' => +'inet.0' }, 'peer-state' => { 'junos: +format' => '206954/222223/0 0/0/0', 'conten +t' => 'Established' }, 'peer-as' => '3000', 'junos:style' => 'terse +', 'elapsed-time' => { 'juno +s:seconds' => '7166196', 'cont +ent' => '11w5d22h' }, 'route-queue-count' => +'0', 'flap-count' => '2', 'input-messages' => '84 +23202', 'output-messages' => '2 +38785' } ], 'down-peer-count' => '0' }, 'xmlns:junos' => 'http://xml.juniper.net/junos/8.0R3/junos' };

Replies are listed 'Best First'.
Re: Complex Hash of Hashes (New to them)
by FunkyMonk (Bishop) on Jul 24, 2007 at 09:13 UTC
    The bgp-peer entry is an array of hashes. You can access them in a similar way that you access a hash of hashes (which you seem to have got the hang of):

    • hash of hash - $xml->{'bgp-information'}->{'bgp-peer'}
    • hash of arrays - $xml->{'bgp-information'}->{'bgp-peer'}->[0]

    Have you read the Perl data structures cookbook? And have a look at Data::Dumper. It's invaluable when dealing with deeply nested structures.

Re: Complex Hash of Hashes (New to them)
by planetscape (Chancellor) on Jul 24, 2007 at 12:57 UTC
Re: Complex Hash of Hashes (New to them)
by citromatik (Curate) on Jul 24, 2007 at 10:28 UTC
    I am trying to print specific parts of the hash, but not with much success using: print $xml->{'bgp-information'}->{'bgp-peer'}{[0]}->{'peer-address'};
    print $VAR1->{'bgp-information'}->{'bgp-peer'}->[0]->{'peer-address'}, +"\n";

    Outputs:

    1.1.1.1

    I am also trying to figure out how I can while loop all the "peer-address" entries (e.g. 1.1.1.1, 2.2.2.2, 3.3.3.3) to print entries within
    for (@{$VAR1->{'bgp-information'}->{'bgp-peer'}}){ print $_->{'peer-address'},"\n"; }

    Outputs:

    1.1.1.1 2.2.2.2 3.3.3.3

    citromatik

Re: Complex Hash of Hashes (New to them)
by leocharre (Priest) on Jul 24, 2007 at 13:04 UTC

    Not sure if this applies. But you remind me of something. I used to have quite heavy complicated hashes, and I could make creepy calls to places like @{ $master{Files}->{$rel_path}->{author}} , etc.. I was comfortable because I knew what the structure was. However.. coming back to code like that months later was torture. Wait, I need to expand on that; coming back to code like that is like fixing html markup for a living using only paper and pencil.

    What's curious to me is that all that stopped when I started taking to object oriented programming. Since then I don't seem to have complex hierarchies of data that way. It seems I create little parts of them on the fly- as they are requested.