http://www.perlmonks.org?node_id=1046404


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