Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Help with logic/syntax

by hdb (Prior)
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


Comment on Re: Help with logic/syntax
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1046404]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2015-07-07 03:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (86 votes), past polls