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

From: Dana Smith <[email-masked]>
Date: Mon, 14 Jan 2002 14:00:54 -0500

1) According to Perl Cookbook, there's Text::Parsewords.

2) On google I found Text::CSV

3) The book "Mastering Regular Expressions" contains this magic:

sub parse_csv {
   my $text = shift;      # record containing comma-separated values
   my @new  = ();

   # the first part groups the phrase inside the quotes.
   # see explanation of this pattern in MRE
   push(@new, $+) while $text =~ m{"([^\"\\]*(?:\\.[^\"\\]*)*)",? |  ([^,]+),? | ,}gx;
   push(@new, undef) if substr($text, -1,1) eq ',';
   return @new;      # list of values that were comma-separated
}

> Dana L. Smith
> Alternate Route Studios
> (919) 531-4116
> [email-masked]
> http://www.altroutestudios.com
> 

-----Original Message-----
From: Jeremy P [mailto:[email-masked]]
Sent: Monday, January 14, 2002 1:09 PM
To: [email-masked]
Subject: [Dev] perl: how to parse quoted comma separated values?

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

_______________________________________________
Dev mailing list
[email-masked]
http://www.trilug.org/mailman/listinfo/dev

---

From: Jeremy P <[email-masked]>
Date: Mon, 14 Jan 2002 14:08:24 -0500 (EST)

> 2) On google I found Text::CSV

Thanks for the tip (thanks to Greg too).  This is what I ended up using...
it's really quite a simple module and works well for my purpose.

>    return @new;      # list of values that were comma-separated

Ugh.  I promise I'm not really "afraid" of regular expressions, but it
stuff like this sure is hard to read!  

--Jeremy

---

From: Jonathan Rippy <[email-masked]>
Date: Mon, 14 Jan 2002 14:20:48 -0500

> stuff like this sure is hard to read!

Both regular expressions and Perl are cool...
unfortunatly, the two combined can quickly 
degenerate into line noise.  :)

Still, you have to love them for their raw
power.  There's a way you can comment the
regular expresion as you are writing it;  I
tend to use that myself.  I can't recall the
syntax right now but it's very useful.

No other real point to this message I guess.

And now for something completely different...

-- 
jonathan rippy