[Denver-pm] Splitting Data
Larry Leszczynski
larryl at emailplus.org
Thu Oct 3 15:42:31 PDT 2013
Hi Robert -
> $String='component1,component2,"This is my, test string", component4';
>
> ($C1, $C2, $Str, $C4) = split(',', $String);
>
> I'm only getting "This is my" in $Str and $C4 does not contain
> "component4". Is there a graceful way of handling this?
It's doing what you asked, namely splitting on any comma it finds - it
does not know that
you do not want it to split in the middle of a double-quoted string...
That line looks like a line you would get in a CSV file, so I would
handle it that way:
use Text::CSV;
my $line = 'component1,component2,"This is my, test string",
component4';
my $csv = Text::CSV->new;
$csv->parse($line) or die $csv->error_diag();
my @columns = $csv->fields();
At this point, @columns contains:
$columns[0]: 'component1'
$columns[1]: 'component2'
$columns[2]: 'This is my, test string'
$columns[3]: ' component4'
HTH,
Larry
More information about the Denver-pm
mailing list