use strict; my %house; my $room; while () { chomp; next unless $_; # skip blank lines if(/^\D/) { $room = $_ } else { $house{$room}{$_}++ } } foreach my $room (keys %house) { print "$room:\n"; print "width height quantity\n"; map { print "$_ $house{$room}{$_}\n"; } sort { $house{$room}{$b} <=> $house{$room}{$a} } keys %{$house{$room}}; } __DATA__ office 1 120 120 120 120 140 135 155 135 120 120 bedroom 2 100 75 100 75 120 180