Like Ted said, it's good to tell us what the code is supposed to do, instead of making us guess. Having said that, I did make a guess - the code seems to me to pick unique combinations of words where there are more than one matches?
This code produces the same results, anyway, and seems to be quicker. Less looping. I don't know what would happen if you had one_one_one and one_one_one, though.
chomp(my @lines = <DATA>);
my %seen;
for my $line1 (@lines) {
for my $line2 (grep $_ ne $line1, @lines) {
if (1 < matches($line1, $line2)) {
$seen{ join ' and ', sort $line1, $line2 } = 1;
}
}
}
print "$_\n" for keys %seen;
sub matches {
my ($line1, $line2) = @_;
my %words1 = map {$_ => 1} split /_/, $line1;
my $matches = 0;
$matches += ($words1{$_}||0) for split /_/, $line2;
return $matches;
}
I'm probably missing something obvious..
edit: s/much quicker/quicker/ :) I made a mistake in my benchmarking.