I am not quite sure if I replicated your expected output.
Read file1 into data struct leaving placeholder for the 2nd value in file2.
Read file2, replace placeholder with 2nd value if key exists otherwise make a 0 for 1st value.
Fiddle with this to get what you want. Basic idea is only read each file once.
Update: Below I used a single string, but an anon array of 2 elements would also get the job done.
use strict;
use warnings;
use Inline::Files;
use Data::Dumper;
$|=1;
my %data;
while (<FILE1>)
{
next unless /^\S/; #skip blank lines
my (@tokens) = (split( " ",$_))[0,1,2,3,6];
my $value = pop @tokens;
$data{"@tokens"} = "$value 0";
}
while (<FILE2>)
{
next unless /^\S/; #skip blank lines
my (@tokens) = (split( " ",$_))[0,1,2,3,6];
my $value2 = pop @tokens;
if ($data{"@tokens"})
{
$data{"@tokens"}=~ s/0$/$value2/;
}
else
{
$data{"@tokens"} = "0 $value2";
}
}
foreach (sort keys %data)
{
print "$_: $data{$_} \n";
}
=prints:
chr10 181243 196457 1: 1 0
chr10 181243 225933 1: 4 3
chr10 181500 225933 1: 15 7
chr10 226069 255828 1: 53 38
chr10 255948 267233 1: 2 0
chr10 255989 267134 1: 65 29
chr10 255989 282777 1: 14 15
chr10 264440 267134 1: 1 0
chr10 267297 282777 1: 0 44
chr10 94055 94554 2: 1 1
chr10 94055 94743 2: 0 1
chr10 94666 94743 2: 0 3
chr10 94853 95347 2: 0 2
chr10 95850 122380 2: 1 0
=cut
__FILE1__
chr10 94055 94554 2 2 1 1 0 23
chr10 95850 122380 2 2 1 1 0 15
chr10 181243 196457 1 1 1 1 0 21
chr10 181243 225933 1 1 1 4 0 21
chr10 181500 225933 1 1 1 15 0 25
chr10 226069 255828 1 1 1 53 0 22
chr10 255948 267233 1 1 1 2 0 12
chr10 255989 267134 1 1 1 65 0 25
chr10 255989 282777 1 1 1 14 0 23
chr10 264440 267134 1 1 1 1 0 25
__FILE2__
chr10 94055 94554 2 2 1 1 0 21
chr10 94055 94743 2 2 1 1 0 20
chr10 94666 94743 2 2 1 3 0 20
chr10 94853 95347 2 2 1 2 0 25
chr10 181243 225933 1 1 1 3 0 21
chr10 181500 225933 1 1 1 7 0 10
chr10 226069 255828 1 1 1 38 0 22
chr10 255989 267134 1 1 1 29 0 24
chr10 255989 282777 1 1 1 15 0 23
chr10 267297 282777 1 1 1 44 0 24
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.