Sparrow, Dave wrote:

I'm reading an Excel .csv file.
Fields are separated by commas.
If a field contains a comma, the whole field is double-quoted (Excel does
this by default).
An example of an input line is as follows:

field1,field2,"field3a, field3b, field3c",field4,"field5a, field5c",field6

I want to get each field into an array.
Quoted fields should be in one element, containing commas, but without
quotes.

Code so far is shown below. It's a bit messy.
Anyone have a better way?
Thanks in advance,
Dave


# Note: $_ contains the whole (chomped) line at this point
while (m/,"(.*?)",/) {
 $a = $`;
 $b = $';
 ($m = $1) =~ s/,/¬/g;  # Just use some temporary character other than a
comma
 $_ = $a.",".$m.",".$b;
}
@arr = split(",");
foreach (@arr) {s/¬/,/g;}
# Done


Take a look at Text::CSV or Text::CSV_XS http://search.cpan.org/author/ALANCITT/Text-CSV-0.01/CSV.pm http://search.cpan.org/author/JWIED/Text-CSV_XS-0.23/CSV_XS.pm









-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to