Hi Monks,

Have hash of hashes need to loop through and add values of 'month' total values and day+week total values of all except for 'Sum'.

my $month_total = 0; my $day_week_total = 0; $VAR1 = { 'Adam' => { 'days' => 22, 'weeks' => 5, 'total' => 22 }, 'Keas' => { 'total' => 114, 'test' => 2, 'weeks' => 8, 'days' => 107, 'months' => 5 }, 'Tim' => { 'total' => 4, 'weeks' => 5, 'days' => 3, 'months' => 1 }, 'Sum' => { 'total' => 440, 'days' => 365, 'months' => 9 } };

Loop through the hash of hashes and add all the 'months' value except for the key 'Sum'. $month_total = 6 ( none for 'Adam' + 5 for 'Keas' + 1 for 'Tim' ) Loop through the hash of hashes and add all the values of 'days'+'weeks' except for the key 'Sum'. $day_week_total = 150 ( (22+5) for 'Adam' + (107+8) for 'Keas' + (3+5) for 'Tim' )

Expected output: $month_total = 6; $day_week_total = 150;

Dear Monks, please give me a direction to achieve this. Thank you so much


In reply to How to loop through hash of hashes and add the values based on condition? by Sami_R

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.