Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
"be consistent"
 
PerlMonks  

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

by poj (Curate)
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


Comment on Re: merging csv files into a third file preserving column & row
Download Code

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 avoiding work at the Monastery: (7)
As of 2014-04-21 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (492 votes), past polls