Please use <code> tags to format your code fix your <code> tags, see How do I post a question effectively? and Markup in the Monastery.

But based on your problem description, here is a template that reads two CSV files of the same length in parallel and writes to a third (which you can then use to replace the second file, if you like) - I think this is what you're after? You can adjust the logic for creating the new rows to fit your needs. It uses Text::CSV, and you should also install Text::CSV_XS for speed.

use warnings; use strict; use Data::Dumper; # Debug $Data::Dumper::Useqq=1; use Text::CSV; my $file1 = 'in1.txt'; my $file2 = 'in2.txt'; my $outfile = 'out.txt'; my $csv = Text::CSV->new({binary=>1, auto_diag=>2, eol=>$/}); open my $ifh1, '<', $file1 or die "$file1: $!"; open my $ifh2, '<', $file2 or die "$file2: $!"; open my $ofh, '>', $outfile or die "$outfile: $!"; $csv->getline($ifh1); # read and discard header $csv->getline($ifh2); # read and discard header $csv->print($ofh, ['col1','col2']); # new header while ( my $row1 = $csv->getline($ifh1) ) { my $row2 = $csv->getline($ifh2); die "file1 has more lines than file2" unless $row2; # do whatever you like to create the output row here my $orow = [@$row1, @$row2]; # example: join the two rows print Dumper($row1, $row2, $orow); # Debug $csv->print($ofh, $orow); } die "file2 has more lines than file1" unless eof($ifh2); close $ifh1; close $ifh2; close $ofh; $csv->eof or $csv->error_diag;

Minor edits.


In reply to Re: Adding an array to an existing csv file as a new column by haukex
in thread Adding an array to an existing csv file as a new column by c.con

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.