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"; } } } }

In reply to Comparing two hashes-help by Gemchal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.