Hi, I am fairly new to Perl and am running into a problem that I hope the Perl Monks can help shed some light on.

I am trying to add a column to an existing file with hundreds of columns and hundreds of thousands of rows. For what it's worth, these files have the same number of rows. The rows also correspond to each other. The file which I am appending a single column has the line number in the first column. (I am appending the second column.)

The following code that works, but it takes forever and I will need to do this daily. I am sure there are several inefficiencies and it seems there has to be a faster way.

use warnings; use strict; open (OUTFILE, ">outfile.csv"); open (IN1, "<infile1.csv"); my $counter = 0; while (<IN1>){ chomp; print OUTFILE $_; open (IN2, "<infile2.csv"); while (<IN2>){ my @f = split(/,/); if ($counter == $f[0]){ print OUTFILE ",$f[1]\n"; }} close IN2; $counter = $counter + 1; } close IN1; close OUTFILE;
Thanks in advance!

Scott

Thanks for all the help. To clarify, the numbered file was in order.

This is the code that seemed to work best (similar to jwkrahn's code).

use warnings; use strict; open my $in2, '<', 'infile2.csv' or die "Cannot open 'infile2.csv' bec +ause: $!"; my %data; while ( <$in2> ) { my ( $key, $value ) = split /,/; $data{ $key } = $value; } close $in2; open my $outfile, '>', 'outfile.csv' or die "Cannot open 'StatsCubeR2f +ormat.csv' because: $!"; open my $in1, '<' , 'infile1.csv' or die "Cannot open 'infile1.csv' be +cause: $!"; while ( <$in1> ) { chomp; print $outfile "$_"; if ( exists $data{ $. - 1 } ) { print $outfile ",$data{$. - 1}\n"; } else { print $outfile ",field_name\n"; } } close $in1; close $outfile;

In reply to Merging larges files by columns by ScottJohn

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.