in reply to adding a new column to a CSV

I don't know who gave you that advice, but version 0.23 is almost 10 years old and full of bugs (by modern standards). If you want to study the current state of CSV parsing, you should look at Text::CSV_XS and Text::CSV. Both have other maintainers than 10 years ago, and both do now mostly what the community expects it to do and more. The XS version is way faster than the pure-perl version, but it is save to always use Text::CSV, as that will use Text::CSV_XS under the hood if it is installed.

wind's paste is fine. I would make small changes to include the relative new auto_diag to catch all errors:

use File::Copy qw(move); use Text::CSV; use strict; use warnings; use autodie; # no need to catch open/read/write/close errors anymore my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, eol => "\n" } +); my $oldfile = "old.csv"; my $newfile = $oldfile . ".tmp"; open my $in, "<", $oldfile; open my $out, ">", $newfile; while (my $row = $csv->getline ($in)) { splice @$row, 1, 0, "New Col 2"; $csv->print ($out, $row); } close $in; close $out;

Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: adding a new column to a CSV
by Anonymous Monk on Apr 23, 2011 at 22:06 UTC
    I am having a bit of trouble with this because it seems to be putting quotes around some of the column values. Using an example tab limited file with 4 lines, the final column on every row apart from the last one is put in quotes

    Also using this code

    splice @$row, 1, 0, "New Col 2";
    the new column text is in quotes which I don't want.

    thank-you

      You obviously did not take the time to read the exhaustive documentation the comes with Text::CSV_XS. By default, fields with spaces are quoted. There is an option (attribute) available to do the opposite:

      quote_space By default, a space in a field would trigger quotation. As +no rule exists this to be forced in CSV, nor any for the opposite, +the default is true for safety. You can exclude the space from +this trigger by setting this attribute to 0.

      Enjoy, Have FUN! H.Merijn