Running this gives me:#!/usr/bin/perl use strict; use warnings; # Deal with first two lines. chomp ($_ = <DATA>); print "$_ Total\n"; chomp ($_ = <DATA>); print "$_ ------\n"; my (@total, $grand_total); <DATA> for 1 .. 2; # Skip first two lines. while (<DATA>) { # Read in line by line chomp; my ($county, @years) = /\d+/g; # Extract numbers from line. my $sum; for (my $i = 0; $i < @years;$i ++) { $sum += $years[$i]; # County totals. $total[$i] += $years[$i]; # Year totals. } printf "%s %10d\n", $_, $sum; # Print line, with county total. } $grand_total += $_ for @total; # Final two lines, print yearly totals. print " ------ ------ ------ ------ ------\n +"; print " Total"; printf " %10d", $_ for @total, $grand_total; print "\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
COUNTY 2001 2002 2003 2004 Total ------ ------ ------ ------ ------ ------ 26023 225 282 272 299 1078 26025 711 796 809 899 3215 26027 250 319 318 313 1200 26059 196 221 242 292 951 26077 1170 1371 1439 1439 5419 26149 355 415 346 367 1483 26159 414 434 504 493 1845 ------ ------ ------ ------ ------ Total 3321 3838 3930 4102 15191
In reply to Re: Calculating subtotals from a table
by Anonymous Monk
in thread Calculating subtotals from a table
by pugsly62
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |