Wow! What a timely tidbit of information. I was just pondering the same problem and your link was most helpful. But as I discovered from the FAQ link, the example doesn't handle extra spacing (around commas) very well.

Here's my 'space fixed' version, with checking for single quotes as well:
use strict; use warnings; # crazy mix of quoting my $test1 = q/, "test with space ",,, mary had , a,, 'cake, with +cheese' , and, "a, \"little" , lamb chop , /; # original FAQ example my $test2 = q/SAR001,"","Cimetrix, Inc","Bob Smith","CAM",N,8,1,0,7,"E +rror, Core Dumped"/; # throw some wild spaces in there my $test3 = q/SAR001, "", "Cimetrix, Inc", "Bob Smith", "CAM",N, 8 ,1, +0 ,7, "Error, Core Dumped"/; # finally a nearly empty string my $test4 = q/,/; split_string($test1); split_string($test2); split_string($test3); split_string($test4); sub split_string { my $text = shift; my @new = (); push(@new, $+) while $text =~ m{ \s*( # groups the phrase inside double quotes "([^\"\\]*(?:\\.[^\"\\]*)*)"\s*,? # groups the phrase inside single quotes | '([^\'\\]*(?:\\.[^\'\\]*)*)'\s*,? # trims leading/trailing space from phrase | ([^,\s]+(?:\s+[^,\s]+)*)\s*,? # just to grab empty phrases | (), )\s*}gx; push(@new, undef) if $text =~ m/,\s*$/; # just to prove it's working print "string: >>$text<<\n"; foreach (@new) { print " part: >>" . (defined($_) ? $_ : '') . "<<\n"; } }

In reply to Re^2: A better way to split CSV files with quoted strings that may contain commas? by ruzam
in thread A better way to split CSV files with quoted strings that may contain commas? by rkaminski

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.