Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by poj (Parson)
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" }
poj

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026460]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2016-06-30 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...











    Results (388 votes). Check out past polls.