Hi all, I have these 4 csv files : File_1.csv
target1,48,12,7 target2,17,16,2 target3,22,6,1
File_2.csv
target5,14,12,8,,3, target6,5,7,9,,,15
File_3.csv
target1,,,,,,13 target2,,,,,,8 target4,,,,11,5,6
File_4.csv
target1,,,,51,8 target2,,,,87,42 target4,22,3,7,,
And I want to merge them all specific to row and column each value is in(preserving column information also). My present code is working fine but its not able to preserve the column information for any value. Here goes my present script
use strict; use warnings; print ("Now merging \n"); my $filenum = 0; my ( %row_val, %data ); 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; $data{$filenum}{$row_val} = \@values; } close $fh; } foreach my $row_val ( sort keys %row_val ) { print $row_val, ",", join( ",", map { $data{$_}{$row_val} ? @{ $da +ta{$_}{$row_val} } : ",," } 1 .. $filenum ), "\n"; }
The merged output(WRONG) generated by above code
target1,48,12,7,,,,,,,,,13,,,,51,8 target2,17,16,2,,,,,,,,,8,,,,87,42 target3,22,6,1,,,,,,,,, target4,,,,,,,,,,11,5,6,22,3,7 target5,,,,14,12,8,,3,,,,,, target6,,,,5,7,9,,,15,,,,,,

-----DESIRED/CORRECT OUTPUT-----

target1,48,12,7,51,8,13 target2,17,16,2,87,42,8 target3,22,6,1,,, target4,22,3,7,11,5,6 target5,14,12,8,,3, target6,5,7,9,,,15

In reply to merging csv files into a third file preserving column & row by zing

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.