Append this after all the calculations:

foreach my $date (keys %counts){ my $overallcount = $overallcounts{$date}; foreach my $word (keys %{$counts{$date}}){ $counts{$date}{$word."_average"} = sprintf("%.2f", 100 * $counts{$ +date}{$word} / $overallcount); } $counts{$date}{"_overallcount"} = $overallcount; } print Dumper \%counts;

Which yields:

$VAR1 = { '2017-09-30' => { 'c' => 1, 'b' => 2, 'a_average' => '57.14', '_overallcount' => 7, 'b_average' => '28.57', 'c_average' => '14.29', 'a' => 4 }, '2017-09-04' => { 'd' => 1, 'c_average' => '18.18', '_overallcount' => 11, 'b' => 3, 'a' => 5, 'b_average' => '27.27', 'd_average' => '9.09', 'c' => 2, 'a_average' => '45.45' } };

You can then replace {$word."_average"} with {$word} to overwrite the original values.


In reply to Re: Calculations using values from two different hashes by FreeBeerReekingMonk
in thread Calculations using values from two different hashes by Maire

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.