my $last_nth; for my $i (keys %history) { last if ($last_nth = $history{ $i }) == ($histcount - $n); }