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;
|
|---|
| 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 | |
by Tux (Canon) on Apr 25, 2011 at 07:07 UTC |