My problem is how to get to substract 25 minus 40.20. How to refer to this data?

If I'm understanding you correctly, that would be

my $diff = $saldi{C} - $saldi{D};
However, looking at the rest of the fine code you've produced already (no irony!), makes me think this can't really be your problem... :)

So, let me guess, the problem is you want to access the values at a point in your program where they're no longer available, e.g. after having completed the foreach $file ( glob "*.txt" ) { ... } loop(?)

In that case - if you want grand totals over all data files - you'll simply have to move the %saldi hash before the outer loop, as already suggested by wfsp -- as you have things now, %saldi will (a) get reinitialised for every new file (so the old values are lost), (b) fall out of scope when the loop is done (also meaning the values are lost).

Otherwise - if you want to keep individual sums per file - you'd have to store those values away in a somewhat more complex data structure, e.g. a hash-of-hashes (HoH). Something like that:

my %saldi; foreach $file ( glob "*.txt" ) { open FILE, '<', $file; my $data = {}; while (<FILE>) { chomp; next unless /\S/; my ($name, $price) = ( split /,/, )[ 3, 4 ]; next unless $name && $price; $data->{$name} += $price; } my ($datasetname) = $file =~ m/^(\S+)\.txt/; print "$datasetname\n"; foreach my $name ( keys %$data ) { printf "\t$name\t" . "%16s\n", big_money( $data->{$name} ); } # store it... $saldi{$datasetname} = $data; close FILE; } # now, you can access all data by specifying the appropriate # pairs of keys, e.g. printf "diff C-D: %.2f\n", $saldi{testdata}{C} - $saldi{testdata}{D}; # ...

HTH :)


In reply to Re^3: Make calculation with values from hash by almut
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.