Gemchal has asked for the wisdom of the Perl Monks concerning the following question:
Hi All, I am new to perl so please be gentle. I am trying to compare two hashes. I have a reference text file (1) and another text file (2) i would like to compare (Eventually i have another 19 to compare to reference) what i want to do is compare the two text files and if there is a key (representing position) eg 245 in both text file 1 and 2 to keep the value from text file 2. Then i want to compare the next line in the files, if the position in text file 1 is missing in textfile 2 i want it to add that position and value to text file two. I hope this makes sense, i have pasted the script that i have written below (but the problem is when i run it i get an identical copy of text file 1 saved as text file two. Hope you can help Thanks Gemma :
#! /usr/bin/perl -w use 5.010; open (REF, "ref_snp.txt"); open (GENOTYPE, "$ARGV[0]"); open (OUT, ">$ARGV[1]"); my %genotype; my %ref; while(<GENOTYPE>) { if (/(\d+)\t\w\t(\w)/) { %genotype=($1=>$2,); my @position=keys %genotype; #foreach $position (sort keys %genotype) # { # print OUT "$position\t$genotype{$position}\n"; # } } else { print "It doesn'y match!$_ not match!\n\n"; } } while (<REF>) { if (/(\d+)\t(\w)/) { %ref = ($1=>$2,); my @position_ref = keys %ref; foreach $position_ref (sort keys %ref) { if (exists $genotype{$position_ref}) { print OUT "$position_ref\t$genotype{$position_ref}\n"; } else { $genotype{$position_ref}=$ref{$position_ref}; print OUT "$position_ref\t$genotype{$position_ref}\n"; } } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Comparing two hashes-help
by toolic (Bishop) on Aug 20, 2010 at 13:39 UTC | |
|
Re: Comparing two hashes-help
by kennethk (Abbot) on Aug 20, 2010 at 13:59 UTC | |
|
Re: Comparing two hashes-help
by MajingaZ (Beadle) on Aug 20, 2010 at 16:13 UTC | |
|
Re: Comparing two hashes-help
by locked_user sundialsvc4 (Abbot) on Aug 20, 2010 at 20:06 UTC |