Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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]
thezip is randomly reminded of Hugo's_house_of_ho rrors

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (15)
As of 2017-03-24 18:20 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (305 votes). Check out past polls.