my %genotype; while(<GENOTYPE>) { $genotype{$1} = $2 if (/(\d+)\t\w\t(\w)/); } while (<REF>) { next unless (/(\d+)\t(\w)/); print OUT (defined($genotype{$1}) and ($genotype ne '')) ? "$1\t$g +enotype{$1}\n" : "$1\t$2\n"; }
Can't verify your regexes without sample data, given the files you dealing with I would have thought that the files might have been formatted the same.
Your for loop when reading your ref file would make lots of extraneous entries. You are building your %ref hash and then every after every addition to it you dump out the entire contents, so your ref file would grow factorially! And unless you actually want that behavior, I see no reason here why you'd bother with the %ref at all.
You appear to just want to update the genotypes located in the first file into your second file. So just put the positions and genotype of the first file into a hash. Then read the ref and see if a located position in the ref file has a value in the genotype hash, if yes then print that other wise print the ref values
However you have not mentioned what you want if a position and genotype exists in your first file that is not in your reference. Perhaps that can never happen? Pending on your dataset size might just for my $file (@files) {open (GENOTYPE, $file); blah blah}

In reply to Re: Comparing two hashes-help by MajingaZ
in thread 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.