G'day zing,

I think you've possibly overcomplicated your solution. You mostly seemed to be on the right track until you got to the innermost loop (of the initial loop); further on, things became very complicated with the second loop that's producing the output. Possibly adding to any confusion are the variables %row_val and $row_val (two instances) giving you code like $row_val{$row_val}.

A further issue is that you haven't fully described how you want your merge to work. For instance, what happens if two or more files have different (non-zero length) data for the same cell. Your sample input and output is insufficient to glean your intent here.

In the code below: I've added logic to specify how the merge should occur (your real-world application may require different logic); replaced %row_val and %data with the single hash %merged; and greatly simplified the output code.

$ perl -Mstrict -Mwarnings -E ' no warnings q{qw}; my @file_data = ( [qw{target1,48,12,7 target2,17,16,2 target3,22,6,1}], [qw{target5,14,12,8,,3, target6,5,7,9,,,15}], [qw{target1,,,,,,13 target2,,,,,,8 target4,,,,11,5,6}], [qw{target1,,,,51,8 target2,,,,87,42 target4,22,3,7,,}], ); use warnings q{qw}; my %merged; for my $file (@file_data) { for my $line (@$file) { my ($row_val, @values) = split /,/ => $line; for (0 .. $#values) { if (! defined $merged{$row_val}[$_] or ( ! length $merged{$row_val}[$_] and length $va +lues[$_] ) ) { $merged{$row_val}[$_] = $values[$_]; } } } } say join q{,} => $_, @{$merged{$_}} for sort keys %merged; ' 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

-- Ken


In reply to Re: merging csv files into a third file preserving column & row by kcott
in thread 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.