Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^4: How to loop through hash of hashes and add the values based on condition?

by Sami_R (Sexton)
on Jan 08, 2020 at 19:39 UTC ( [id://11111200]=note: print w/replies, xml ) Need Help??


in reply to Re^3: How to loop through hash of hashes and add the values based on condition?
in thread How to loop through hash of hashes and add the values based on condition?

Hi PRYRT,

Thank you so much for the direction and detailed explanation, its really helped to figure out the problem and fixed it, thanks again. I am novice to perl will catch up soon.

use strict; 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 $name ( sort keys %{$VAR1} ) { for my $calen ( sort keys %{ $VAR1->{$name} } ) { if ( $name ne 'Sum' ) { if ( ($calen eq 'days') || ($calen eq 'weeks') ){ $day_week_total += $VAR1->{$name}->{$calen} if ($VAR1->{ +$name}->{$calen}); } elsif ( ($calen eq 'months') ){ $month_total += $VAR1->{$name}->{$calen} if($VAR1->{$nam +e}->{$calen}); } } } } print "\n day_total: $day_week_total —-- month_total: $month_total \n" +;

Output: day_total: 150 —-- month_total: 6

  • Comment on Re^4: How to loop through hash of hashes and add the values based on condition?
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11111200]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-25 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found