foreach $key (sort keys %ytd ){ ...
No strict? ... watch out for the Inquisitors!
I'll presume that you declared $key earlier. ;)
Seriously, i want to point out that anytime i see variables like $t1, $c1, ,$d1, etc., i try to remove these and use 'containers' instead. Here is my take on the problem:
I think that shoving the number items into an array is more convenient and allows you to use the array in the format instead of the individual scalars.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 .
Without knowing the problem, i can't really comment on your datastructure %ytd, but it might benefit you in terms of maintenance to combine each of the 'dN' and 'cN' pairs into one that contains an array. For example:
Just food for thought. :)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] }, );
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
In reply to (jeffa) Re: Question on using format and write
by jeffa
in thread Question on using format and write
by Popcorn Dave
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |