Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Help with logic/syntax

by hdb (Monsignor)
on Jul 25, 2013 at 18:09 UTC ( #1046404=note: print w/replies, xml ) Need Help??

in reply to Help with logic/syntax

IMHO the best data structure is an array of hashes. Each time you encounter a hostname line, you push a new hash onto the array and store the hostname. For each of the following lines you can refer to that hash by $array[-1] until the next hostname line. See below:

use strict; use warnings; my @results; while(<DATA>){ push @results, {'hostname' => $1} if /^(hostname.*)/; # push + a new hash into the array $results[-1]->{'vlan'}+= split(/\s+/) - 3 if /^vlan/; # numb +er of words - 3 $results[-1]->{'ifconfig'}++ if /^ifconfig/; # one +for each ifconfig } for( @results ) { print "Hostname : $_->{'hostname'}\n"; print "# of vlans: $_->{'vlan'}\n"; print "# of ifconfig lines: $_->{'ifconfig'}\n\n"; } __DATA__ # hostname 123 hostname 123 vlan create vif1 1 2 3 4 5 6 vlan add vif1 7 8 ifconfig vif1-1 some data here ifconfig vif1-2 some data here ifconfig vif1-9 some data here # hostname 456 hostname 456 vlan create vif5 1 2 vlan add vif5 3 4 5 vlan add vif5 6 7 8 9 ifconfig vif5-1 some data here ifconfig vif5-2 some data here ifconfig vif5-9 some data here

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1046404]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2018-03-19 05:53 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (232 votes). Check out past polls.