----- Original Message ----- From: "John McCormick"



i'm trying to figure out how to split a file delimited
by commas and newlines.
@data = split (/\n|\,/, <infile>)
the only problem is that some of the data fields are
strings enclosed in double quotes, and within some of
those double quotes are more commas.  that's too
tricky for me.  how do i do that?
thanks!
court

Hi John

There is a module that comes bundled with Perl, Text::ParseWords. A sample program below that you can copy and run. (Include the __DATA__ line and the 2 lines that follow).

#!/usr/bin/perl
use strict;
use warnings;
use Text::ParseWords;

my @data;
while(<DATA>) {
chomp;
push @data, parse_line ",", 0, $_;
}

print join "\n", @data;

__DATA__
one,word,"couple words, with comma","another word"
"string, with, commas",parse_words

parse_line, one of the available functions, takes 3 arguments: the delimiter (the comma), a flag, and the line to parse.

You may want to read up by running the line below at your command prompt:
perldoc Text::ParseWords

Season's best
Chris




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to