Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

For loop: Hash

by nickt9999 (Acolyte)
on Jul 02, 2013 at 08:21 UTC ( #1041977=perlquestion: print w/replies, xml ) Need Help??
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'} = '' ; ### 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'} = '' ; ### 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


Replies are listed 'Best First'.
Re: For loop: Hash
by hdb (Prior) on Jul 02, 2013 at 08:47 UTC

    My principles are:

    • for plain lists use arrays (or array references),
    • for lists with named attributes use hashes (or hash references).
    So both on the router level and on the peer level you have plain lists, otherwise you have named attributes. So I would propose the following, which also allows for a simpler iteration over the structure:

    use strict; use warnings; my $router_data = [ # array of hashes for each router { # hash for router 1 routerName => 'asr01', ipAddr => '', bgpPeer => [ # array of hashes for each peer { # hash for first peer Name => 'PEER1', ASN => '111', prefixList => 'PREFIX-PEER1-OUT', }, { # hash for second peer Name => 'PEER2', ASN => '222', prefixList => 'PREFIX-PEER2-OUT', }, ], }, { # hash for router 2 routerName => 'asr02', ipAddr => '', bgpPeer => [ # array of hashes for each peer { # hash for first peer Name => 'PEER1', ASN => '333', prefixList => 'PREFIX-PEER1-OUT', } ], }, ]; for my $router ( @$router_data ) { print $router->{routerName}." : \n"; for my $peer ( @{ $router->{bgpPeer} } ) { print $peer->{prefixList}."\n"; } }
      This is a great answer. To expand on it, the OP mentions that he is new to Perl, and perhaps an early programmer (or not, I don't know). Once you start using this layout, and you are finding that you need more logic and functionality against a Router, or a Peer, then it will be easy to break that part of the structure out into a class of its own.

      Many thanks for this.

      I have used this as the way forward.



Re: For loop: Hash
by Happy-the-monk (Abbot) on Jul 02, 2013 at 08:58 UTC

    No objection to what hdb said above.
    If you have to use the exact data structure you presented above, this might be the way to go:

    for my $i ( sort keys %{$router_href} ) { for my $j ( sort keys %{$router_href->{$i}->{bgpPeer}} ) { print "($i:$j) " , $router_href->{$i}{bgpPeer}{$j}{Name} , ":\t" , $router_href->{$i}{bgpPeer}{$j}{prefixList} , "\n" } }

    Cheers, Sören

    (hooked on the Perl Programming language)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1041977]
Approved by hdb
Front-paged by Corion
[robby_dobby]: Every time I fall asleep, there's a small guy in the dreams, shouting "Whoo!" and it jolts me awake. :/
[Lady_Aleena]: robby_dobby, at least you aren't driving. I seem to always be driving somewhere in my dreams and end up at a weird house.
[robby_dobby]: LA: That's there. But this is work and it's the same thing as falling asleep driving :-)
[Lady_Aleena]: I love the dream where I was at a huge house and decided to go swimming in the pool, but then the tour group showed up.
choroba fell asleep while driving half a year ago
[choroba]: fortunately, only the traffic sign and the car were damaged

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2017-05-29 08:08 GMT
Find Nodes?
    Voting Booth?