Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Merging larges files by columns

by lidden (Curate)
on Sep 16, 2011 at 21:04 UTC ( #926485=note: print w/replies, xml ) Need Help??

in reply to Merging larges files by columns

If think you want something like this. (untested)
open my $OUTFILE, ">", "outfile.csv" or die "oh noo: $!"; open my $IN1, "<", "infile1.csv" or die "bummer1: $!"; open my $IN2, "<", "infile2.csv" or die "bummer2: $!"; while (<$IN1>){ chomp; print $OUTFILE $_; my $line2 =<$IN2>; chomp $line2; my @f = split/,/, $line2; print $OUTFILE ",$f[1]\n"; } close $IN2 or die $!; close $IN1 or die $!; close $OUTFILE or die $!;

Replies are listed 'Best First'.
Re^2: Merging larges files by columns
by Kc12349 (Monk) on Sep 16, 2011 at 21:06 UTC

    I was about to post my very similar code. I would point out that if you are new to perl and not set in your ways, try to use lexical file handles as in my example as opposed to barewords.

    use autodie; open (my $out, '>', 'outfile.csv'); open (my $in1, '<', 'infile1.csv'); open (my $in2, '<', 'infile2.csv'); while ( defined (my $line = <$in1>) ) { chomp $line; print {$out} $line; chomp(my $in2_line = <$in2>); my @f = split m/,/, $in2_line; print {$out} ',' . $f[1] . "\n"; } close $in1; close $in2; close $out;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://926485]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2017-12-12 21:08 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (339 votes). Check out past polls.