use strict; use warnings; use Time::HiRes qw/time/; my ( @data1, @data2 ); push @data1, [rand(10), rand(10), rand(10), rand(10)] for 1 .. 100_000; push @data2, [rand(10), rand(10), rand(10), rand(10)] for 1 .. 100; for my $point2 ( @data2 ) { my %nearest = ( distance => 1_000_000, point => [0,0,0,0], ); for my $point1 ( @data1 ) { next if abs( $point1->[0] - $point2->[0] ) > $nearest{distance}; next if abs( $point1->[1] - $point2->[1] ) > $nearest{distance}; next if abs( $point1->[2] - $point2->[2] ) > $nearest{distance}; next if abs( $point1->[3] - $point2->[3] ) > $nearest{distance}; my $distance = sqrt( ($point1->[0]-$point2->[0]) ** 2 + ($point1->[1]-$point2->[1]) ** 2 + ($point1->[2]-$point2->[2]) ** 2 + ($point1->[3]-$point2->[3]) ** 2 ); if ( $distance < $nearest{distance} ) { $nearest{distance} = $distance; $nearest{point } = [ @$point1 ]; } } #print $nearest{distance} . "\n"; }