in reply to Splitting a comma-delimited string where a substring could contain commas
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
|
|---|
| 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 | |
by mrbbking (Hermit) on May 05, 2002 at 00:39 UTC | |
by belg4mit (Prior) on May 05, 2002 at 01:18 UTC |