in reply to Splitting a line on just commas

Text::CSV_XS but ... if your sample is indicative:

$s = 'a,b,"hey, you","str1, str2, str3",end';; print for split ',(?=\S)', $s;; a b "hey, you" "str1, str2, str3" end

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: Splitting a line on just commas
by Anonymous Monk on Jun 14, 2010 at 16:37 UTC

    This of course fails in the following cases, because it's looking for the space after the comma:

    a,b,"hey,you",etc a, b, "hey, you", etc

    Which is why it was qualified with "if your sample is indicative". A more general solution can be found by focusing on the fields themselves, rather than the commas:

    @fields = $s =~ /("[^"]*"|[^,]*),/gc; $lastfield = $s =~ /\G(.*)/; push @fields, $lastfield;

    But even that has no provision for placing a quotation mark inside a quoted string, and I'm sure there are other things I missed. The problem is hairier than it looks, hence, Text::CSV or Text::CSV_XS is best.

      A more general solution can be found

      The phrase "more general" is similar to "a bit pregnant".

      There is little point in catering for one possibility not in evidence and not another. You should either cater for what is; or for every possibility.

      As noted on wikipedia, there is no single consistent standard for what constitute CSV or TSV etc. The module we both mentioned therefore jumps through extraordinary hoops to try and cater for every possible variation--and inevitably fails.

      But, individual sources of CSV output are usually self-consistent.

      Just as we don't take a universal phrase book covering every known language--were such available--with us when travelling to a particular country, it rarely makes sense to try and cater for non-evident possibilities unless you are going to cater for them all. You're either doing more work than necessary; or not enough.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.