This is a variation on a faq -- see perlfaq 4, "How can I count the number of occurrences of a substring within a string?".
Using a little eval
trickery :
foreach (<DATA>) {
my ($w1, $w2) = split / /, $_;
my %common = ();
foreach ( split //, $w1){
eval "\$foo=\$w2=~tr/$_/$_/" unless exists $common{$_};
$common {$_}=1 if $foo;
}
print "$w1 and $w2 have ", join (", ",sort keys %common)," in comm
+on\n";
}
__DATA__
perl monk
help temp
frood hoopysdf
bilbo baggins
jibber jabber
I wonder about your algorithm in score2, though. It seems to return 1 regardless of actual matches because of the
last. Perhaps you need a larger/ more complex word set?