nickt9999 has asked for the wisdom of the Perl Monks concerning the following question:
Hi
I am a newbie to Perl. I am after some advice on if what I have come up with is the best way.
I have a hash ref
my $router_href = {}; ### Router 1 $router_href->{1}{'routerName'} = 'asr01' ; $router_href->{1}{'ipAddr'} = '1.1.1.1' ; ### BGP Peer : 1 $router_href->{1}{'bgpPeer'}{1}{'Name'} = 'PEER1' ; $router_href->{1}{'bgpPeer'}{1}{'ASN'} = '111'; $router_href->{1}{'bgpPeer'}{1}{'prefixList'} = 'PREFIX-PEER1-OUT' ; ### BGP Peer : 2 $router_href->{1}{'bgpPeer'}{2}{'Name'} = 'PEER2'; $router_href->{1}{'bgpPeer'}{2}{'ASN'} = '222' ; $router_href->{1}{'bgpPeer'}{2}{'prefixList'} = 'PREFIX-PEER2-OUT' ; ### Router 2 $router_href->{2}{'routerName'} = 'asr02' ; $router_href->{2}{'ipAddr'} = '2.2.2.2' ; ### BGP Peer : 1 $router_href->{2}{'bgpPeer'}{1}{'Name'} = 'PEER1' ; $router_href->{2}{'bgpPeer'}{1}{'ASN'} = '333'; $router_href->{2}{'bgpPeer'}{1}{'prefixList'} = 'PREFIX-PEER1-OUT' ;
I would like to iterate through the routers and print the prefixList
I dont know if my data structure is the best way of doing it, I am open to suggestions :-)
This is what I have managed to come up with
my $rtrId = '1'; for my $rtr ( keys %{$router_href}) { print $router_href->{$rtrId}{routerName} . " : " . "\n"; my $rplId = '1'; for my $pfxList ( keys %{$router_href->{$rtrId}{bgpPeer}} ) { print $router_href->{$rtrId}{bgpPeer}{$rplId}{prefixLi +st} . "\n"; $rplId++; } $rtrId++ }
Thanks in advance
Nick
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: For loop: Hash
by hdb (Monsignor) on Jul 02, 2013 at 08:47 UTC | |
by admiral_grinder (Pilgrim) on Jul 02, 2013 at 16:27 UTC | |
by nickt9999 (Acolyte) on Jul 04, 2013 at 10:24 UTC | |
Re: For loop: Hash
by Happy-the-monk (Canon) on Jul 02, 2013 at 08:58 UTC |
Back to
Seekers of Perl Wisdom