Hello c.con, and welcome to the Monastery!

haukex has shown you the correct way to process CSV files using Text::CSV. I just want to comment on your style of variable declarations.

(1) For Perl versions 5.12.0 and above, use VERSION; enables use strict; — but unfortunately your use 5.10.0; does not. The absence of an explicit use strict at the head of your script makes it difficult to catch typos. In particular, the line push @arr, @col2; references the variable @arr which is not declared or used elsewhere in the code. This is likely a typo for push @array, @col2; — an error which use strict; would have caught for you.

(2) It is good practice to declare each variable as near as possible to its point of first use. So, instead of:

my ($fh1, $fh2, $fh3, $fh4, $fh5); ... open ($fh1, '<', $file1) or die $!;

just declare $fh1 where it is first used:

open (my $fh1, '<', $file1) or die $!;

If you edit your code according to this principle you will see that the variables $fh5, @col1, @col3, and $lines3 are never used. More importantly, you will immediately see that between the declaration and first use of $fh4 this filehandle is never, in fact, opened for writing!

Programming is hard enough already; why code in a style that makes it harder than it needs to be? ;-)

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Adding an array to an existing csv file as a new column by Athanasius
in thread Adding an array to an existing csv file as a new column by c.con

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.