Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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 all is calm...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2018-03-17 21:14 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (226 votes). Check out past polls.