shubham.at8 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks..

I'm new to Perl and is working on huge .csv files. My aim is to generate some data by taking some of the fields as input.

For eg. FullName,Age to FullName,FirstName,LastName,Age

The main thing in it is to create 2 new columns after the first one. I do not want to write it into another file rather want to make amendments in the same file. Is there any way to do this? Also, please tell me if there is anyway to increase the number of rows. Please help.

  • Comment on How to add Rows/Columns in .csv using Tie::Handle:CSV

Replies are listed 'Best First'.
Re: How to add Rows/Columns in .csv using Tie::Handle:CSV
by ww (Archbishop) on Jun 12, 2011 at 12:30 UTC
    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.

      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