in reply to Extracting common words

The first while loop is working correctly: it extracts the words from the first text file, removes non-alphanumeric characters, converts the words to lower case, and stores them as the keys in a hash. (Here, as often in Perl, the value stored with the key is irrelevant.) So far, so good. (Note: the line $line =~ s/[[:punct:]]//g; isn’t needed, because all punctuation characters are removed in the subsequent substitution: $word =~ s/[^A-Za-z0-9]//g;.)

The second while loop is more complicated, and that’s where things fall apart. I don’t think I follow the intended logic here; but, rather than try to fix it, it will be simpler — and clearer — to re-think the algorithm.

The easiest approach is to simply repeat the logic of the first while loop and create a second hash containing the words in the second text file. (It will help if you rename the first hash %results to something like %words1, then the second hash can be named %words2.) You now have only to find which keys are common to both hashes, and that will give you the desired result:

my $counter = 0; for my $key (sort keys %words1) { if (exists $words2{$key}) { ++$counter; print $key, "\n\n"; } } print "Found $counter words in common\n";

You may also benefit from studying the FAQs in Data: Hashes (Associative Arrays).

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Extracting common words
by AppleFritter (Vicar) on Oct 22, 2015 at 09:34 UTC

    The easiest approach is to simply repeat the logic of the first while loop and create a second hash containing the words in the second text file. (It will help if you rename the first hash %results to something like %words1, then the second hash can be named %words2.) You now have only to find which keys are common to both hashes, and that will give you the desired result:

    This is good advice; there's also List::Compare::Functional, which may make it easier to get the intersection of the two, e.g. (untested):

    use feature qw/say/; use List::Compare::Functional qw/get_intersection/; # ... my @common = get_intersection( [keys %words1], [keys %words2] ); my $counter = scalar @common; # output common words say join "\n\n", @common; # output count; say "Found $counter words in common."

    EDIT: of course it's $counter, not $common; thanks Athanasius. (See what I meant about "untested"?)