Hi, I want to insert a new column along with the column header in an existing csv.

e.g. I have below csv:

vm_name, vm_cluster, vm1, ha1, vm2, ha2, vm3, ha3,

I am using DBD::CSV in my script to generate above mentioned file

Now, I want to add a 3rd column in above file to make it look like below:

vm_name, vm_cluster, Zone, vm1, ha1, zone2, vm2, ha2, zone3, vm3, ha3, zone1,

I tried doing below with the DBD::CSV but it did not work:

my $table = "mapping"; my $sth = $dbh->prepare ("INSERT INTO $table (Zone) VALUES (?,)" ); $sth->execute ( '<whatever_zone>');

but I get below error:

Execution ERROR: No such column 'Zone' called from dbd1.pl at 29

So I am thinking that DBD::CSV needs column name present already. So, I added the column name manually to the original csv file and ran above code and still it failed with same error.

Could you guide on how to get a column inserted in csv along with the column name. Also, just so you know, the values in this 3rd column will depend on some comparison of the respective values in column 1 and column 2 of the row. I'm hoping DBD::CSV should be able to do that because AFAIK Text::CSV does not have a mechanism to deal with column names.

Thanks.


In reply to DBD::CSV - How to insert a new column with column header in an existing CSV file by slayedbylucifer

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.