use Inline::Files; push @b, [split ' '] for ; while (@a = split ' ', ){ --$_ for @a; print "@{$b[0]}[@a]\n"; for $i (1.. $#b) { undef @h{@{$b[$i]}[@a]}; $s++ if scalar keys %h == 1; undef %h; } print "$s\n" if $s; undef $s; } __FILEA__ 1 2 3 4 1 2 3 5 1 2 4 5 1 3 4 5 2 3 4 5 __FILEB__ A B C D E 0 0 0 + 0 + 0 + + + 0 + + + + 0 0 + + 0 + + + + + 0 + + + + + 0 + + 0