in reply to How to add Rows/Columns in .csv using Tie::Handle:CSV

Your question is unclear.

But your desire to add two new columns after the first without writing a new file needs more thought.

Your language suggests you think there's some way to change the content of a file (on disk, in RAM) without writing it out to storage again. Sure, you could overwrite the existing file (for some types of changes) but doing so would destroy the original file (and under most OSen write the new version would be written in a different spot on the disk or at a different set of addresses in RAM, anyway). So take advantage of Perl's capabilities (and no, I don't mean the "edit in place" operation, which merely disguises the fact that you are writing a new file).

One standard technique is to write the revised material to a temp file, and, when complete, renamethe original to original.bak (or delete the original) and rename the temp file to the original name. Alternately, write the revised material to a file with a name that indicates its status.

Leaving the actual code for file opening, reading, writing as an exercise, the options above lead to how the question of how to do the insert_processing : Here's one (iterative for clarity) possibility.

#!/usr/bin/perl use strict; use warnings; use 5.012; my @array = qw (a c e); for my $item(@array) { say $item; } say "-" x 10; my $save = $array[1]; my $insert = "b"; splice @array,1,1, ($insert, $save); for my $item(@array) { say $item; } say "-" x 10; $save = $array[3]; $insert = "d"; splice @array,3,1, ($insert, $save); for my $item(@array) { say $item; }

All that said, if what it is you plan to insert and the meaning of "Age to FullName" have any real bearing on your problem, you need to clarify your SOPW.

Replies are listed 'Best First'.
Re^2: How to add Rows/Columns in .csv using Tie::Handle:CSV
by shubham.at8 (Initiate) on Jun 12, 2011 at 16:20 UTC
    Sorry for being unclear
    Actually what I wanted is that I can make change in a already existing .csv file without creating a new file. But it seems that I should create a new file as it would be safer.
    Thanks anyways. :)

      And DBD::CSV is not an option either, as SQL::Statement does not support ALTER TABLE foo ADD COLUMN ... yet.


      Enjoy, Have FUN! H.Merijn