use strict; use warnings; my @results; while(){ push @results, {'hostname' => $1} if /^(hostname.*)/; # push a new hash into the array $results[-1]->{'vlan'}+= split(/\s+/) - 3 if /^vlan/; # number 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