Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: comparing two sets of data

by ColonelPanic (Friar)
on Nov 28, 2012 at 09:20 UTC ( #1005987=note: print w/replies, xml ) Need Help??

in reply to comparing two sets of data

It appears that the "key" is the entire line of file1. If so, then this is a pretty easy task.

Here is an example:

use Modern::Perl; my %keys; open my $file1, '<', 'filename1.txt' or die "Can't open file 1: $!"; open my $file2, '<', 'filename2.txt' or die "Can't open file 2: $!"; #read in the first file. while (<$file1>) { chomp; #remove newline. $keys{$_}++; #Add the key to your hash } #find keys in the second file. while (<$file2>) { foreach my $key (keys %keys) { say "$key matches" if /\b\Q$key\E\b/; } }

Regex Notes: \b matches a word boundary. This is because you presumably wouldn't want "John Smith" to match "John Smithfield". \Q...\E is the quote literal modifier. If your key contains characters with a special meaning in regexes (such as the dot in "Mr. Smith") you want to match only the literal characters.

Yes, you do end up needing a nested loop...that is intrinsic to the nature of your task.

When's the last time you used duct tape on a duct? --Larry Wall

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005987]
[Eily]: oh, boolean context uses the "" overload if no bool overload has been defined
[Eily]: (I wondered if overloading bool was actually necessary)
[Corion]: Yeah, you need bool to get a true value, and the rest to return the other value ;)
[choroba]: perl -wE '{package o; use Tie::Scalar; use parent -norequire => "Tie::StdScalar" ;sub FETCH { "x" x int rand 2}} tie my $x, "o" ; say $x for 1 .. 10'
[choroba]: for complete ness

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2017-07-27 13:48 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (414 votes). Check out past polls.