for my $one (0..$#@dist-1) { my $two = $one; while (++$two < @dist && $dist[$two]-$dist[$one] <= $cutoff) { print "found pair: ($x[$one],$y[$one],$z[$one]) and ($x[$two],$y[$two],$z[$two])\n" if sqrt(($x[$one]-$x[$two])**2+($y[$one]-$y[$two])**2+($z[$one]-$z[$two])**2) <= $cutoff; } }