Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I used UNIX to join 2 files based on the first column, but now i want to use it on windows, so UNIX won't work anymore. I wrote a script to do it in Perl, but this takes for ever. I read something about hashes and that it should be faster this way. Could somebody help me with this? The two files have a different nr of columns and rows and when column 1 is equal the two files should be merged. The files are tab separated. What I did in Unix was just sort the files and join. My code in Perl looks like this:
my $file1 = $ARGV[0]; my $file2 = $ARGV[1]; open(first_file,'<', $file1) or die $!; my @FILE1 = <first_file>; close(first_file); open(sec_file,'<', $file2) or die $!; my @FILE2 = <sec_file>; close(sec_file); @RESULTS; for my $line(@FILE1){ my($ID, @values) = split("\t", $line); for my $sec_line(@FILE2){ my($ID2, @values2) = split("\t", $sec_line); if($ID eq $ID2){ push (@RESULTS, "$ID @values @values2"); } } } open(RESULTS,'>','results.txt') or die $!; foreach(@results){ print RESULTS "$_\n"; } close(RESULTS);
Could somebody help me do this on a faster way? Thanks!

In reply to Join files using perl by linseyr

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    [karlgoethebier]: marto: "Wat kümmert mich ming Jeschwätz von jestern?" (allegedly by a former german chancellor)
    [marto]: My German is very limited and Google translate really isn't helping with that one :P

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (8)
    As of 2018-01-19 13:35 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (218 votes). Check out past polls.