You can simplify all the dereferencing if you use a while loop with each rather than a for loop. With this method, you cannot sort the keys, but in your case, that is OK.
use strict;
use warnings;
use Test::More tests => 2;
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
}
};
while (my ($name, $hash) = each %$VAR1) {
next if $name eq 'Sum';
if (exists $hash->{months}) {
$month_total += $hash->{months};
}
if (exists $hash->{days} and exists $hash->{weeks}) {
$day_week_total += ($hash->{days} + $hash->{weeks});
}
}
ok($month_total == 6, 'month_total');
ok($day_week_total == 150, 'day_week_total');
OUTPUT:
1..2
ok 1 - month_total
ok 2 - day_week_total
|