Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found