Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: How to find the difference between two text files using a hash array

by roboticus (Chancellor)
on Jan 20, 2013 at 21:41 UTC ( #1014343=note: print w/replies, xml ) Need Help??

in reply to How to find the difference between two text files using a hash array


This is a pretty common question, so if you look at the perlfaq docs (specifically see "How can I remove duplicate elements from a list or array" and the next few articles in perlfaq4), it'll show you what you need to know.

Since a hash lets you access a value given a key, you typically do it by first loading your hash with the values you want. As you read the second file, you can check whether the value is in the hash or not.

$ cat #!/usr/bin/perl use strict; use warnings; my %H = (a=>1, d=>2); while (<DATA>) { s/\s+$//; if (exists $H{$_}) { print "$_ exists in hash\n"; } else { print "$_ missing from hash\n"; } } __DATA__ a b c d

Running this script should give you:

$ perl a exists in hash b missing from hash c missing from hash d exists in hash


When your only tool is a hammer, all problems look like your thumb.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2016-10-25 23:25 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (330 votes). Check out past polls.