my ($location, $h); while(){ chomp; $location = $_ if m/^[a-zA-Z]/; next unless m/(\d+)\s+(\d+)/; $h->{$location}->{windows}++; $h->{$location}->{frames}->{"$1x$2"}++; $h->{$location}->{total_length}+= $1*2 + $2*2; } use Data::Dumper; print Dumper $h; my $fixed_cost_per_window = 100; my $cost_by_material = 0.5; my $total; for my $location( keys %$h ) { my $fixed = $h->{$location}->{windows} * $fixed_cost_per_window; my $materials = $h->{$location}->{total_length} * $cost_by_material; printf "%-10s\t%d windows \$%7.2f\tmaterials \$%7.2f\ttotal \$%7.2f\n", $location, $h->{$location}->{windows}, $fixed, $materials, $fixed+$materials; $total += $fixed+$materials; } printf "\nTotal: \$%.2f\n", $total; __DATA__ office 1 120 120 120 120 140 135 155 135 120 120 bedroom 2 100 75 100 75 120 180