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

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11111061 use warnings; my $month_total = 0; my $day_week_total = 0; my $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 } }; for my $key ( grep $_ ne 'Sum', keys %$VAR1 ) { $month_total += ($VAR1->{$key}{months} // 0); $day_week_total += $VAR1->{$key}{days} + $VAR1->{$key}{weeks}; } print "month_total = $month_total day_week_total = $day_week_total\n";
  • Comment on Re: How to loop through hash of hashes and add the values based on condition?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to loop through hash of hashes and add the values based on condition?
by Sami_R (Sexton) on Jan 08, 2020 at 19:45 UTC
    Hi Tybalt89,

    Thank you so much for the different approach.