in reply to hash comparison
First off, a couple of style issues:
With that somewhat in mind consider the following:
use strict; use warnings; use List::Util; my %candidate = CountWords ('the the the the the the the'); my %reference = CountWords ('the cat is the on the mat'); my %counts = map {$_ => List::Util::min ($candidate{$_} || 0, $reference{$_} || + 0)} keys %candidate, keys %reference; print "$_: $counts{$_}\n" for sort keys %counts; sub CountWords { my ($sentence) = @_; my @words = split (/\W/, $sentence); my %wordCount; ++$wordCount{$_} for @words; return %wordCount; }
Prints:
cat: 0 is: 0 mat: 0 on: 0 the: 3
|
|---|