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

In reply to Re: Make calculation with values from hash by GrandFather
in thread Make calculation with values from hash by GertMT

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.