in reply to Calculating subtotals from a table
use strict; use warnings; my ($county,@years) = split /\s+/, <DATA>; my $x = <DATA>; my $county_total; my $year_total; while(<DATA>){ chomp; my($county,@year_county_data) = split /\s+/; $county_total->{$county} += $_ for @year_county_data; foreach (0..$#years){ my $year= $years[$_]; my $data = $year_county_data[$_]; $year_total->{$year} += $data; } } print "County Total\n"; foreach my $county (sort keys %{$county_total}){ print "$county\t", $county_total->{$county},"\n"; } print "====\n"; print "Yearly Total\n"; foreach my $year (sort keys %{$year_total}){ print "$year\t", $year_total->{$year},"\n"; } __DATA__ COUNTY 2001 2002 2003 2004 ------ ------ ------ ------ ------ 26005 432 505 547 555 26021 910 984 1026 1077 26023 225 282 272 299 26025 711 796 809 899 26027 250 319 318 313 26059 196 221 242 292 26077 1170 1371 1439 1439 26149 355 415 346 367 26159 414 434 504 493
|
|---|