in reply to Make calculation with values from hash

It looks like some of your problem can be resolved by including the file name as a key:

#!/usr/bin/perl use strict; my %saldi; my $file; while (<DATA>) { chomp; next unless $_; $file = $1, next if /(\w+\.txt)$/; # Required for sample code only my @cellen = ( split /,/, )[ 3, 4 ]; next unless $cellen[0] && $cellen[1]; $saldi{$file}{ $cellen[0] } += $cellen[1]; # file name as primary +key } my $dSum = 0; my $cSum = 0; my $deltaSum = 0; foreach my $filename ( keys %saldi ) { $cSum += $saldi{ $filename }{ C }; $dSum += $saldi{ $filename }{ D }; my $delta = $saldi{ $filename }{ D } - $saldi{ $filename }{ C }; $deltaSum += $delta; printf "$filename delta:\t%8s\n", big_money( $delta ); } printf "C sum: \t%16s\n", &big_money( $cSum ); printf "D sum: \t%16s\n", &big_money( $dSum ); printf "Delta sum:\t%16s\n", &big_money( $deltaSum ); sub big_money { #Learning Perl my $number = sprintf "%.2f", shift @_; #Add one comma each time though the do-nothing loop 1 while $number =~ s/^(-?\d+)(\d\d\d)/$1,$2/; #Put the dollar sign in the right place $number =~ s/^(-?)/$1/; $number; } __DATA__ Data_1.txt 394,eur,2006,D,18.20 394,eur,2006,D,22 394,eur,2006,C,25 Data_2.txt 494,eur,2006,C,25 494,eur,2006,D,79

Prints:

Data_1.txt delta: 15.20 Data_2.txt delta: 54.00 C sum: 50.00 D sum: 119.20 Delta sum: 69.20

Note that the two files have been munged together into a DATA section to avoid reliance on external files for the sample code.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Summarizing tabular data, was: Re^2: Make calculation with values from hash
by GertMT (Hermit) on Jan 04, 2007 at 17:35 UTC
    Thanks to all who gave a hint into what direction I should work. All great examples given as well. Finally I've got something working which I'll post below. I'm still a bit puzzled why I need to put the closing bracket of the Foreach loop higher by using real datafiles
    } # real datafiles my ($basename) = ( $filename =~ m/^(\S+)\.txt/ ) or warn;
    as opposed to when I make use of the sample data
    printf " D + C: \t%15s%16s\n", &big_money( ( $saldi{$filename}{'"D"'} * -1 ) + ( $saldi{$filename}{'"C"'} + ) ), &big_money($deltaSum); } #sample
    but it works! Script below makes use of sample data. And only has a problem when irregular pattern appears to be somewhere in the middle of the file (which should never be the case).
    It thaugt me a great deal about hashes and how you need to refer to the data.
    thanks all, Gert
    #!/usr/bin/perl -w use strict; use diagnostics; use vars qw ! %saldi $filename !; #chdir # real code # '/Users.......' # real code # or die "Cannot open : $!"; #real cod +e #foreach my $filename ( glob "*.txt" ) { # real code # open FILE, '<', $filename or warn "Can't open FILE: $!";#real cod +e # while (<FILE>) { #real cod +e # chomp; #real cod +e # next unless $_; #real cod +e while (<DATA>) { # Required for sample code chomp; # Required for sample code next unless $_; # Required for sample code $filename = $1, next if /(\w+\.txt)$/; # Required for sample code my @fields = ( split /,/, )[ 0, 1, 2, 3, 4 ]; my ( $account, $currency, $year, $dofc, $amount ) = @fields; next unless $dofc && $amount; $saldi{$filename}{$dofc} += $amount; # file name as primary + key } my $dSum = 0; my $cSum = 0; my $deltaSum = 0; foreach $filename ( keys %saldi ) { $cSum += $saldi{$filename}{'"C"'}; $dSum += $saldi{$filename}{'"D"'} * -1; my $delta = ( $saldi{$filename}{'"D"'} * -1 ) + $saldi{$filename}{'"C"'} +; $deltaSum += $delta; #} real my ($basename) = ( $filename =~ m/^(\S+)\.txt/ ) or warn; printf "$basename\n"; printf "\tD sum: \t%16s", &big_money( $saldi{$filename}{'"D"'} * -1 ) . "\n"; printf "\tC sum: \t%16s", &big_money( $saldi{$filename}{'"C"'} ) . + "\n"; printf " D + C: \t%15s%16s\n", &big_money( ( $saldi{$filename}{'"D"'} * -1 ) + ( $saldi{$filename}{'"C"'} + ) ), &big_money($deltaSum); } #sample # close FILE or warn "Can't close FILE: $!"; # real code #} # real code sub big_money { #Learning Perl my $number = sprintf "%.2f", shift @_; #Add one comma each time though the do-nothing loop 1 while $number =~ s/^(-?\d+)(\d\d\d)/$1,$2/; #Put the dollar sign in the right place $number =~ s/^(-?)/$1/; $number; } __DATA__ Data_1.txt 394,eur,2006,"D",18.20 394,eur,2006,"D",22 394,eur,2006,"C",25 394,eur,2006,"C",16 Data_2.txt 494,eur,2006,"C",25 494,eur,2006,"D",79 494,eur,2006,"D",79 494,eur,2006,"D",79 494,eur,2006,"C",100 494,eur,2006,"C",1