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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.