sub score { my ($word1, $word2) = @_; my (%chars1, %chars2) = (); $chars1{$_}++ for split '', $word1; $chars2{$_}++ for split '', $word2; # the minimum of the two hashes is the number in common for each letter my $sum = 0; $sum += ($chars1{$_} < $chars2{$_} ? $chars1{$_} : $chars2{$_}) for keys %chars1; return $sum; } while () { chomp; print "$_: " . score(split /\s+/) . " in common\n"; } __DATA__ perl monk help temp frood hoopy bilbo baggins jibber jaber