[Dev] perl: how to parse quoted comma separated values?

Jeremy P dev@trilug.org
Mon, 14 Jan 2002 13:09:18 -0500 (EST)


Hi TriLug dev types,

Hope this question is appropriate for this forum.  I'm not a real
developer-type, but I use scripting to help various system admin tasks.  
For a particular task, I need to parse in records from a CSV
(comma-separated values) file using perl.  The data files have records
like this:

"King, Jr","Martin","Luther","etc"
"Washington","George",,"foo"

I'd like to each record into an array, getting rid of the double-quotes
and the ',' delimiters in the process.

Obviously a simple statement like this won't work:
	@fields = split(',');
because of the fields with commas in the data ("King, Jr")

I can't do things that match on '","' because that wouldn't apply to the
empty fields or the first and last fields.

Anyone done simple parsing like this before?  Is there something glaringly
obvious that I'm missing?  I poked around on CPAN and there are a bunch of
CSV routines, including even a DBD driver, but that seems way overkill.  
Maybe I should just delve into using one of those modules.

Thanks for any advice,

Jeremy Portzer
Durham Tech Community College