This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Compare pairs of key/value of perl hash tables

Hi Guys,

Does anyone know how to compare pairs of key/value in two hashtables ? I'm currently working with two tab files. Each one contains list of SNPs with their location and position as information. I need to compare both files and get the common location/position between two files. Input files looks like :

-File1:

LOCATION             POSITION 
LOC105032014         221                 
LOC105032014         222                 
LOC105032014         371                 
LOC105032014         434                 
LOC105032014         1271

-File2:

LOCATION             POSITION          
LOC105032014         193                 
LOC105032014         371                 
LOC105032014         1097                
LOC105032014         1102                
LOC105032014         1111                
LOC105032014         1119                               
LOC105032014         1271

My output should give something like:

LOCATION             POSITION 
LOC105032014         1271                
LOC105032014         371

Any help will be welcome. Thanks !

perl snps hashes

It seems your key is both, the location and the position. I would use a hash like $hash{"$location:$position"} = 1.

Not so much an issue here, probably, but watch out for duplicate keys when you make a custom key like this.

4 answers

You're looking for lines that are common between two files. Ther are plenty of solutions on the net already. You could use comm command or if you want to use perl and both files fit in memory, you can read then into arrays and use List::Compare.

list::compare is great

I would rather prefer to solve it in easy and quicker way,

grep -w -Ff File2.txt File1.txt > commonFile1File2.txt

Sorry if I understood your question wrong.

You got it right. And it worked as well as the perl script. Such a magic trick. Thank you. :)

I wrote a script that suggests one approach:

Usage:

$ intersect.pl --fileA="A.txt" --fileB="B.txt" > answer.txt

This uses Perl's experimental "smartmatch" feature, which can give an annoying warning message that can be discarded by directing standard error to /dev/null. If you're not comfortable using experimental features, there is a List::Util library that offers limited set-style operations.

It exactly gave me what I wanted. Thank you so much for your help. :)

You could iterate through one hash, and for each gene ID (location) key in the hash, check if it is present in the other hash, and whether the values are the same, and print out the Location and Position when you find matching values. But you would have to make hashes of arrays, because hash keys in perl have to be unique values, so each gene ID could only be present once as a hash key. This may not be the most efficient way to go about this.

Log in to answer this question.