in reply to How to use split() in perl to ignore white space and ','
Hello iamnewbie, and welcome to the Monastery!
It looks as though your data is in CSV (comma separated values) format, in which case the best approach is to use a dedicated CSV module. For example:
#! perl use strict; use warnings; use Text::CSV_XS; my $testb = "hello,'world, yo',matt"; my $csv = Text::CSV_XS->new({ keep_meta_info => 1, quote_char => "' +" }); my @records; if ($csv->parse($testb)) { my @fields = $csv->fields; for my $col (0 .. $#fields) { if ($csv->is_quoted($col)) { push @records, $csv->{quote_char} . $fields[$col] . $csv->{quote_char}; } else { push @records, $fields[$col]; } } } else { warn "parse() failed on argument: ", $csv->error_input, "\n"; $csv->error_diag; } print "$_\n\n" for @records;
Output:
16:47 >perl 1156_SoPW.pl hello 'world, yo' matt 16:47 >
(Code adapted from the documentation for Text::CSV_XS.)
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to use split() in perl to ignore white space and ','
by iamnewbie (Novice) on Feb 11, 2015 at 07:30 UTC | |
by soonix (Chancellor) on Feb 11, 2015 at 10:01 UTC | |
by Not_a_Number (Prior) on Feb 11, 2015 at 09:43 UTC | |
by brilant_blue (Beadle) on Feb 11, 2015 at 10:03 UTC | |
by karlgoethebier (Abbot) on Feb 11, 2015 at 14:31 UTC |