in reply to Re: Make calculation with values from hash
in thread Make calculation with values from hash

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