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

In reply to Summarizing tabular data, was: Re^2: Make calculation with values from hash by GertMT
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.