for my $i (0 .. $#existing_wells){ for my $j (0 .. 100){ # or perhaps (0 .. min(100, $#existing_wells - $i) if($existing_wells[$i + $j] == $j + 1){ print qq( ...