Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How can I split a comma-delimited string when the fields can have commas in them?

by dbetz (Initiate)
on Mar 20, 2000 at 20:48 UTC ( [id://5722]=perlquestion: print w/replies, xml ) Need Help??

dbetz has asked for the wisdom of the Perl Monks concerning the following question:

Normally I would use split on ',', but that will not work because of the comma embedded within certain fields. Is there a "perl" way to parse this into its proper fields?

"R", "2164", "27-2164", "270102", "Add Terminal Server to John, Jane, and George's PC's", "3/13/00", "3/27/00", "00/00/00", "02:00:00", "Jane Doe", " 3 - Released", "Jane Doe"

Edited by davido: Added code tags and more legible formatting.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How can I split a comma-delimited string when the fields can have commas in them?
by chromatic (Archbishop) on Mar 20, 2000 at 23:18 UTC
    The best answer is to use Text::CSV from CPAN. Otherwise, you'll have to craft a regex which can handle obscure cases like commas between quotes, escaped quotes within quotes, and other funny stuff like that.

    One possibility is:

    $string =~ m!"*?([^,])"*?(?:=,)!;
Re: How can I split a comma-delimited string when the fields can have commas in them?
by turnstep (Parson) on Mar 29, 2000 at 22:13 UTC

    Here's an answer from 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 "([^\"\\]*(?:\\.[^\"\\]*)*)",? | ([^,]+),? | , }gx; push(@new, undef) if substr($text, -1,1) eq ','; return @new; ## list of values that were comma-spearated } ## Use like this: @goodlist = parse_csv($csvlist);

    Ugly, to be sure, but the complexity level really kicks up a notch when you add the delimiters into the fields themselves. Also, the above snippet allows quotes inside the fields, as long as they are backslashed.

Re: How can I split a comma-delimited string when the fields can have commas in them?
by Anonymous Monk on Mar 08, 2002 at 18:07 UTC
    How about
    pop:s/^"//;chop;split'","';
    It doesn't handle any escaped quotes but you end with the data in @_
    David Pardo

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://5722]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-23 17:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found