in reply to Splitting a comma-delimited string where a substring could contain commas

My first thought was Text::CSV as well, but I'm not sure it'll help you here. You don't have true 'comma separated values' format. CSV does not use parens to group items - it uses a single character. Parens work in pairs.

If you have any control over the format, you might consider changing it to match the CSV spec - something standard. Then Text::CSV will help you. The example below is only slightly modified from the examples in the POD

#!/usr/bin/perl -w use strict; use Text::CSV_XS; while( <DATA> ){ my $line = $_; my @input; my $csv = Text::CSV_XS->new({ # defaults are: ["]["][,][0] quote_char => '"', escape_char => '"', sep_char => ',', binary => 0 }); if( $csv->parse( $line ) ){ @input = $csv->fields; } else { my $err = $csv->error_input; warn "Text::CSV_XS->parse() failed in line $. on argument '" , $err, "'\n"; } foreach my $item (@input){ print "$item\n"; } print "\n"; } # first line parses 'correctly' - second does not. __DATA__ this,that,those,"these (not enough, nope, never)",there this, that, those, these (not enough, nope, never), there
  • Comment on Re: Splitting a comma-delimited string where a substring could countain commas
  • Download Code

Replies are listed 'Best First'.
Re: Re: Splitting a comma-delimited string where a substring could countain commas
by belg4mit (Prior) on May 03, 2002 at 20:41 UTC
    Excluding embeded paranthesis (which the poster did not mentioned), tr%()%""% would solve the grouping problem for CSV.

    --
    perl -pew "s/\b;([mnst])/'$1/g"

      If the whole field were in parenthesis, you're right, that would work.

      But the value is this:
      , these (not enough, nope, never),
      ...not this...
      , (these not enough, nope, never),

      Text::CSV_XS chokes if you replace the parens with your tr/// suggestion. CSV requires that either the whole field or none of the field be quoted - you can't quote part of a field.

        Good call. A little massaging would overcome that, but then that puts us in the realm of s/// and others have already posted better solutions using that.

        --
        perl -pew "s/\b;([mnst])/'$1/g"