in reply to Reading and writing CSV

I agree with the comments that DBI would be overkill. DBI is a great module, but get some more experience with more basic perl before trying to tackle it.

The Perl Cookbook has a recipe for parsing comma-separated data. This recipe (1.15) is taken from Mastering Regular Expressions. (My apologies if posting this is inappropriate).

BEGIN QUOTED MATERIAL:

Use the procedure in Mastering Regular Expressions.

sub parse_csv { my $text = shift; # record containing comma-separated values my @new = (); push(@new, $+) while $text =~ m{ # the first part groups the phrase inside the quotes. # see explanation of this pattern in MRE "([^\"\\]*(?:\\.[^\"\\]*)*)",? | ([^,]+),? | , }gx; push(@new, undef) if substr($text, -1,1) eq ','; return @new; # list of values that were comma-separated }
Or use the standard Text::ParseWords module.
use Text::ParseWords; sub parse_csv { return quotewords(",",0, $_[0]); }
END QUOTED MATERIAL

The Perl Cookbook is a must-have book for any perl programmer. If you don't have a copy, please buy one. It has helped me immensely.

Replies are listed 'Best First'.
Re: Re: Reading and writing CSV
by Juerd (Abbot) on Jun 04, 2003 at 16:26 UTC