Hey I have a script below, which takes two files, loops over the lines in the files, and if two certain variables match, combines the files into an output file. It works fine, except I am starting to loop over very large files, and this loop is now taking days instead of minutes. Is there a way to make this more efficient?

#!/us
/bin/perl
use File::Basename;
open (DATA, "file1.out") or die "$!";
while (my $line = <DATA>){
if ($line =~ /(\S+)(\s+)run(\d+)_sub(\d+)_event(\d+)(\s+)(\S+)(\s+)(\S+)(\s+)(\S+)(\s+)(\S+)(\s+)(\S+)(\s+)(\S+)(\s+)(\S+)(\s*)/){
open (REFILE, ">>output.out");
my $var1 = $5;
my $var2 = $9;
my $var3 = $11;
my $var4 = $13;
my $var5 = $15;
my $var6 = $17;
my $var7 = $19;
my $var8 = $7;
my $var9 = $1;
open (DATA2, "file2.out") or die "Cannot open file2";
while (my $line2 = <DATA2>){
if($line2 =~ /(\S+)(\s+)(\S+)(\s+)run(\d+)_sub(\d+)_event(\d+)(\s*)/){
my $var10 = $3;
my $var11 = $7;
if ($var3 == $var10){
if ($var4 == $var11){
print REFILE "$var1 $var2 $var11\n";
} } } }
close(DATA2);
} } close(REFILE); close(DATA);

Original content restored by GrandFather


In reply to 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.