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

I have the following code:

for my $line (@lines) { print "Line: $line\n"; if ($csv->parse($line)) { my @fields = $csv->fields(); my $i = 0; for my $column (@fields) { print ++$i, " = ", $column, "\n"; } print "\n"; } else { my $error = $csv->error_input(); print "parse() failed on argument: ", $error, "\n"; } }

This works fine when @lines contains the data in the format:

field1, field2, field3

But prints parse() failed on argument:(no $error value) for the following:

"field1", "field2", "field3"

The Text::CSV docs say "A field within CSV may be surrounded by double-quotes." So it would appear to me to be valid formatting. Could someone explain the error to me? Thanks.

Replies are listed 'Best First'.
Re: Text::CSV field quoting problem
by steves (Curate) on Jun 23, 2003 at 02:01 UTC

    The spaces after the commas are what's throwing it off. Without the quotes, it just assumes those are part of the next field.

      You are correct, it works now. Thanks :)

Re: Text::CSV field quoting problem
by barbie (Deacon) on Jun 23, 2003 at 08:37 UTC
    Note that Text::CSV probably isn't the best CSV parsing module to use. There are a large number of characters that it considers to be illegal, e.g. UK pound symbol, £, among many others. I've doctored my copy to handle them, but others have recommended the use of Text::CSV_XS and Text::xSV if you want more out of a CSV parser.

    --
    Barbie | Birmingham Perl Mongers | http://birmingham.pm.org/