There are a number of major problems with your code. The first is that you can open REFILE many times, but you only explicitly close it once - that probably indicates a fundamental logic error.

Any code which rereads a file for each line of another file is bound to be slow. Don't do that! Instead read all of the smaller file into memory (if you want to look stuff up put that stuff in a hash) once before you start processing the second file, then make use of the cached data from the smaller file.

Numbered variables almost always indicates that you should be using an array. In this case you could:

my @vars = (undef, $5, $9, $11, $13, $15, $17, $19, $7, $1);

although given that you don't access most of the values in the sample code you may be better to use named variables (with sensible names) for just the fields you do need. You would be even better to not capture the fields you're not interested in and thus simplify your regex!

Ignoring the file management issue for the moment (I don't know how big the files are so it's hard to tell what a sensible solution is), the code can be cleaned up to:

use strict; use warnings; my $file2 = <<FILE2; 00001 003 run1_sub1_event4 FILE2 while (my $line = <DATA>) { next if $line !~ /\S+\s+run\d+_sub\d+_event(\d+)\s+(.*)/; my ($event, $tail) = ($1, $2); my @params = split /\s+/, $tail; open my $DATA2, '<', \$file2 or die "Cannot open file2"; while (my $line2 = <$DATA2>) { next if $line2 !~ /\S+\s+(\S+)\s+run\d+_sub\d+_event(\d+)/; print "$event $params[1] $2\n" if $params[2] == $1 && $params[ +3] == $2; } close $DATA2; } __DATA__ 00001 run1_sub1_event1 1 2 3 4 5 6 7

prints:

1 2 4
True laziness is hard work

In reply to Re: quicker way to merge files? by GrandFather
in thread quicker way to merge files? by nessundorma

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.