my ($total, $count); while (<$fh>) { # omitting any error checking here - you shouldn't omit it, though. my ($id, $date, $value) = split ' '; if ($date eq $desired_date) { $total += $value; ++$count; } } my $avg = $total / $count;