Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Extracting common words

by AppleFritter (Vicar)
on Oct 22, 2015 at 09:34 UTC ( [id://1145629]=note: print w/replies, xml ) Need Help??


in reply to Re: Extracting common words
in thread Extracting common words

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"?)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1145629]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-19 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found