parse CSV review

From: Jeff Thies (jeff_at_spamalanadingong.com)
Date: 07/19/04


Date: Mon, 19 Jul 2004 07:08:10 GMT


   I have nothing against Text::CSV except that it can be a pain
coercing recalcitrant sysadmins to install modules. This can take days
to never.

A while back I asked about glueing CSV records back together that
contain linebreaks. The crux of this is that valid records contain even
numbers of double quotes.

   It's occured to me that a simlar strategy would work for CSV fields.

sub parseCSVLine{
my $line=shift;
my @fields=();my $field=''; # initialize

my @fragments=split(/,/,$line,-1); # split into "," seperated fragments

foreach my $nibble(@fragments){

if($field){$field .= ','} # add the missing commas;

$field .= $nibble; # combine fragment into a field until...

my $count = $field =~tr/"/"/; # count quotes

unless($count % 2){ # ...there's an even number of double quotes

$field =~s/""/"/g; $field=~s/^\s*"//g; $field=~s/"\s*$//g; # fix quotes
# fix quotes

push @fields, $field;
$field=''; # reinitialize $field
                }
        }
return @fields;
}

It all seems too easy. Does this seem like a reasonable approach?

Have I missed something? I'm unsure of that fix quotes line.

BTW that wild parse CSV regex that's floating around doesn't seem to
work on my data. It's a bit too complex for me to figure out why not!

   Jeff