foreach my $i ( 0 .. 1520) { foreach my $j ( $i + 1 .. 1520 ) { my $dist = sqrt( ($arrayx[$i] - $arrayx[$j])**2 + ($arrayy[$i] - $arrayy[$j])**2 + ($arrayz[$i] - $arrayz[$j])**2 ); ## Adding $i and $j to your output will let you know what that print "$i <> $j : $dist\n"; } } exit 0;