G'day hrholmer,

Welcome to the Monastery.

[This post was difficult to read, which you acknowledge. For future reference, avoid prosaic descriptions and this chatty style you've adopted. Choose concise and succinct statements to describe your problem; use short dot points instead of drawn out paragraphs; put code in blocks and use pseudocode if don't know what syntax you need.]

The following script, pm_1207659_csv_append.pl, provides some techniques which you may find useful (assuming I've got the basic idea of what you're trying to accomplish).

#!/usr/bin/env perl -l use strict; use warnings; use autodie; use Text::CSV; use Inline::Files; my $csv = Text::CSV::->new(); my $csv_file = 'pm_1207659_out.csv'; my %seen; open my $csv_fh, '<', $csv_file; while (my $row = $csv->getline($csv_fh)) { $seen{$row->[0]} = 1; } close $csv_fh; my @in_fhs = (\*FILE1, \*FILE2, \*FILE3); open my $out_fh, '>>', $csv_file; while (1) { my @data = map scalar readline $_, @in_fhs; last unless defined $data[0]; chomp @data; my $key = (split /_/, $data[0])[1]; next if $seen{$key}++; $csv->print($out_fh, [$key, @data[1 .. $#in_fhs]]); } close $out_fh; __FILE1__ A_B B_B C_A B_C C_D __FILE2__ F2-1 F2-2 F2-3 F2-4 F2-5 __FILE3__ F3-1 F3-2 F3-3 F3-4 F3-5

Notes:

Here's a sample run with before and after data:

$ cat pm_1207659_out.csv X,Y,Z C,what,ever some,thing,else $ pm_1207659_csv_append.pl $ cat pm_1207659_out.csv X,Y,Z C,what,ever some,thing,else B,F2-1,F3-1 A,F2-3,F3-3 D,F2-5,F3-5

You should be able to follow this through and see how duplicate data (existing and new) is handled.

— Ken


In reply to Re: Appending single record to CSV (or TDV, not too late to switch) - filling 13 fields from 13 files, one of which is split into array of 2 and I just need half of it... by kcott
in thread Appending single record to CSV (or TDV, not too late to switch) - filling 13 fields from 13 files, one of which is split into array of 2 and I just need half of it... by hrholmer

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.