Actually Text::CSV handles quoted fields pretty well, but the default quote character is ". See the quote_char section of the Text::CSV. Note that the sep_char is a single character though, so spaces are not removed, which would make 101, '1997' invalid CSV, because the quote_char should be the next thing after the sep_char. To allow spaces, set allow_whitespace to 1:
use strict; use Text::CSV; use Data::Dumper; my @lines=<DATA>; my $csv = Text::CSV->new ({sep_char => ',' , quote_char => "'", allow +_whitespace => 1}); my @AoA; for (@lines) { if (/^\((.+?)\).?/){my $con=$1; if ($csv->parse($con)) { my @fields = $csv->fields(); push @AoA,[@fields]; } else { warn "Line could not be parsed: $_\n"; } } } print Dumper \@AoA; __DATA__ (101, '1997-02-25', 'S1', 31.00, NULL, 0.00, 'this becomes two fields, + so no go', 5.11), (102, '1998-03-26', 'S1', 31.00, NULL, 0.00, 'this will remain one fie +ld', 6.11),
$VAR1 = [ [ '101', '1997-02-25', 'S1', '31.00', 'NULL', '0.00', 'this becomes two fields, so no go', '5.11' ], [ '102', '1998-03-26', 'S1', '31.00', 'NULL', '0.00', 'this will remain one field', '6.11' ] ];
In reply to Re: Text::CSV - parsing
by Eily
in thread Text::CSV - parsing
by sphalaris
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |