use List::Util qw/sum min/; use List::MoreUtils qw/pairwise/; sub distance { my ($i, $j) = @_; no warnings; my $distance = sum pairwise { $a != $b } @$i, @$j; return $distance; } sub distance_to_any { my ($i, $l) = @_; return min ( map distance( $i, $_ ), @$l ); } print distance ([1, 2, 3, 4], [1, 8, 5, 7]) . "\n"; print distance_to_any ( [1, 2, 3, 4], [ [1, 8, 5, 7], [1, 2, 3, 9] ] )