I\'m working on a project that involves parsing a large csv formatted file in Perl and am looking to make things more efficient.
My approach has been to split(
Here is a version that also respects quotes (e.g. foo,bar,"baz,quux",123 -> "foo", "bar", "baz,quux", "123").
sub csvsplit {
my $line = shift;
my $sep = (shift or ',');
return () unless $line;
my @cells;
$line =~ s/\r?\n$//;
my $re = qr/(?:^|$sep)(?:"([^"]*)"|([^$sep]*))/;
while($line =~ /$re/g) {
my $value = defined $1 ? $1 : $2;
push @cells, (defined $value ? $value : '');
}
return @cells;
}
Use it like this:
while(my $line = ) {
my @cells = csvsplit($line); # or csvsplit($line, $my_custom_seperator)
}