Hey guys, Need a small favour from you gurus out there. I have 3 text files below.
This is one.txt
chromosome1 50000 12 20
chromosome2 20000 0 21
chromosome3 41444 9 2
chromosome4 21414 4 1

This is two.txt
chromosome1 50000 41 51
chromosome2 20000 1 20
chromosome3 41444 2 11
chromosome6 12141 12 22

This is three.txt
chromosome1 50000 11 2
chromosome2 20000 3 22
chromosome3 41444 2 15

The first column is the chromosome number. The 2nd column is the position. The 3rd column is the value1 and 4th column is value2.
What i am trying to do is to read the first file, one.txt, add the $lines to hash and then open the 2nd file, two.txt, see if exists. If exists, add the value1 of the 2nd file to the 1st file and value2 of the 2nd file to first file.
I need to do a loop, till the last line of each file and then print all the $lines and the final values for value1 and value2. Value 1 and value 2 should not be sum up together.
This is the code I got so far but it doesn't give the right output as what i want.
#!/usr/bin/perl -w #declare all filenames $filename1 = 'one.txt'; $filename2 = 'two.txt'; $filename3 = 'three.txt'; #open text file 1 open (FILE1, $filename1) or die "Unable to open $filename1 because $!\ +n"; while ($line = <FILE1>) { chomp ($line); ($chrX, $chrpos, $value1, $value2) = split (/\t/, $line); $key1 = join ("_", $chrX, $chrpos); $hash{$key1}++; }; close FILE1; open (FILE2, $filename2) or die "Unable to open $filename2 because $!\ +n"; while ($line = <FILE2>) { chomp ($line); ($chrX, $chrpos, $value11, $value22) = split (/\t/, $line); $key2 = join ("_", $chrX, $chrpos); if (exists $hash{$key2} > 0) { $hash{key2} = $value11 + $value1; $hash{key2} = $value22 + $value2; $hash{key2}++ } }; close FILE2; open (FILE3, $filename3) or die "Unable to open $filename3 because $!\ +n"; while ($line = <FILE3>) { chomp ($line); ($chrX, $chrpos, $value111, $value222) = split (/\t/, $line); $key3 = join ("_", $chrX, $chrpos); if (exists $hash{$key3} > 0) { $hash{key3} = $value111 + $value11; $hash{key3} = $value222 + $value22; $hash{key3}++ } }; print "$hash{key3} $value111 $value222"; close FILE2;
Sorry, its my first time posting, i hope I did it right. And thanks in advance for the help. :)

The final output i am looking for is something like this
chromosome1_50000 64 73 chromosome2_20000 4 63 chromosome3_41444 13 35 chromosome4_21414 4 1 chromosome6_12141 12 22

In reply to How to add values of hash by reading from different text files by faozhi

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.