I have 2 huge txt files, the first column is common to both of them, the second file includes more records that the first one, I want to read the first column in the second file element by element and chech in the first column of the first file also element by element to find a match, when a match is found I want to print to a new file only the matched rows from both files, I wrote the following Perl code which works but it is very slow and memory expensive, could anybody help me to find better way or fix my code please:
use strict; use warnings; use Getopt::Std; open (my $newfile, ">", "C:/result.txt"); open (my $fh, "<", "C:/position.txt"); open (my $file, "<", "C:/platform.txt"); my @file_data = <$fh>; my @position = <$file>; close($fh); close($file); foreach my $line (@position) { my @line = split(/\t/,$line); my $start = $line[0]; foreach my $values(@file_data) { my @values = split(/\t/, $values); my $id = $values[0]; if ($start eq $id) { print $newfile $line[0],"\t",$line[2],"\t",$line[3],"\t",$values[0 +],"\t",$values[1],"\t",$values[2],"\n"; } } } print "DONE";

In reply to match text files by Salwrwr

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.