use strict; use warnings; my @students = ( [qw(cap shortsleeve jeans leather flipflop)], [qw(beenie longsleeve shorts cloth highheel)], [qw(cap tanktop shorts rope flat)], [qw(beenie longsleeve shorts cloth flipflop)] ); my @best = ( -1 ); while( my $x = shift @students ) { Y: for my $y (@students) { my $diff = @$x; for( my $i = 0; $i<@$x; $i++ ) { $diff-- if $x->[$i] eq $y->[$i]; next Y if $diff <= $best[0]; } @best = ( $diff, $x, $y ); } } print "@$_\n" for @best[1,2];