http://www.perlmonks.org?node_id=982618

slayedbylucifer has asked for the wisdom of the Perl Monks concerning the following question:

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.