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

In reply to Re: open input files once by Marshall
in thread open input files once by v15

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.