use strict; my (@stuff,$cust,$total); my %ytd = ( foo => { d1 => 1, d2 => 2, c1 => 3, c2 => 4 }, bar => { d1 => 5, d2 => 6, c1 => 7, c2 => 8 }, baz => { d1 => 9, d2 => 1, c1 => 2, c2 => 3 }, qux => { d1 => 4, d2 => 5, c1 => 2, c2 => 4 }, ); foreach $cust (sort keys %ytd) { my @tot = map { $ytd{$cust}->{"d$_"} - $ytd{$cust}->{"c$_"} } 1..2; $total = do { my $t; $t += $_ for @tot; $t }; @stuff = ( $ytd{$cust}->{d1}, $ytd{$cust}->{c1}, $tot[0], $ytd{$cust}->{d2}, $ytd{$cust}->{c2}, $tot[1], ); write; } format STDOUT = @<<<<<<<<<<<<<<<<<<<< $cust @#####.## @#####.## @#####.## @#####.## @#####.## @#####.## @stuff @#####.## $total . #### my %ytd = ( foo => { d => [1,2], c => [3,4] }, bar => { d => [5,6], c => [7,8] }, baz => { d => [9,1], c => [2,3] }, qux => { d => [4,5], c => [2,4] }, );