foreach my $month (@calendar) { my $count = 0; my $total = 0; foreach my $value ( grep { $_ != 0 } map { $hoursWorked{$_}{$month} } @members ) { $total += $value; $count ++; } $avg{$month} = $count ? $total/$count : 0; }