Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: merging csv files into a third file preserving column & row

by poj (Prior)
on Apr 01, 2013 at 10:51 UTC ( #1026460=note: print w/replies, xml ) Need Help??

in reply to merging csv files into a third file preserving column & row

You could change the data structure and merge the values early at the input stage like this. It assumes only one value in any one column. Then the print stage becomes more straight forward.
foreach my $file ( sort glob("*.csv") ) { $filenum++; open my $fh, "<", $file or die $!; while ( my $line = <$fh> ) { chomp $line; my ( $row_val, @values ) = split /,/, $line; $row_val{$row_val} = 1; # delete $data{$filenum}{$row_val} = \@values; # add these 3 lines for my $c (1..@values){ $data{$row_val}[$c-1] .= $values[$c-1]; } } close $fh; } foreach my $row_val ( sort keys %row_val ) { print join ",",$row_val,@{$data{$row_val}},"\n" }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026460]
[erix]: it could be interesting to know whether that 57M customer data breach at ueber was before or after the database switch
[erix]: or perhaps it is unrelated

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2017-11-22 14:05 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (324 votes). Check out past polls.