hank1
asked on
split on comma, UNLESS comma is within ""
Excel outputs .csv file.
If the sheet cell contained a comma, the cell is quoted.
.csv output:
cellone,celltwo,"cell has, comma",cell four
If the sheet cell containes a quote, it's repeated
excell cells:
cell"one celltwo
.csv output
cell""one,celltwo
How can you split on a 'comma'? I can delete the double second quote later.
If the sheet cell contained a comma, the cell is quoted.
.csv output:
cellone,celltwo,"cell has, comma",cell four
If the sheet cell containes a quote, it's repeated
excell cells:
cell"one celltwo
.csv output
cell""one,celltwo
How can you split on a 'comma'? I can delete the double second quote later.
ASKER
I don't think I want to add characters to intermediate results.
They may also be there. :-)
The more I look at this, the more it appears its a little program.
They may also be there. :-)
The more I look at this, the more it appears its a little program.
See
perldoc -w split
perldoc -w split
>I don't think I want to add characters to intermediate results.
>They may also be there. :-)
>The more I look at this, the more it appears its a little program.
I am not sure you fully understand. My example above adds nothing to any intermediate results. It temporarily masks the commans inside of double quotes so split() does not split on them and then changes them back for further processing.
>They may also be there. :-)
>The more I look at this, the more it appears its a little program.
I am not sure you fully understand. My example above adds nothing to any intermediate results. It temporarily masks the commans inside of double quotes so split() does not split on them and then changes them back for further processing.
#this does not work
my $string = '"cellone",celltwo,cellthr ee,"cell, has, comma",cell::four';
$string =~ s/"(.*?)\,(.*?)"/$1::$2/g;
for (split(/\,/, $string)) {
s/\:\:/,/g;
print $_,"\n";
}
perldoc -q split
How can I split a [character] delimited string except when inside
[character]? (Comma-separated files)
Take the example case of trying to split a string that is
comma-separated into its different fields. (We'll pretend you
said comma-separated, not comma-delimited, which is different
and almost never what you mean.) You can't use "split(/,/)"
because you shouldn't split if the comma is inside quotes. For
example, take a data line like this:
SAR001,"","Cimetrix, Inc","Bob Smith","CAM",N,8,1,0,7,"Er ror, Core Dumped"
Due to the restriction of the quotes, this is a fairly complex
problem. Thankfully, we have Jeffrey Friedl, author of a
highly recommended book on regular expressions, to handle these
for us. He suggests (assuming your string is contained in
$text):
@new = ();
push(@new, $+) while $text =~ m{
"([^\"\\]*(?:\\.[^\"\\]*)* )",? # groups the phrase inside the quotes
| ([^,]+),?
| ,
}gx;
push(@new, undef) if substr($text,-1,1) eq ',';
If you want to represent quotation marks inside a quotation-
mark-delimited field, escape them with backslashes (eg, "like
\"this\"". Unescaping them is a task addressed earlier in this
section.
Alternatively, the Text::ParseWords module (part of the stan-
dard Perl distribution) lets you say:
use Text::ParseWords;
@new = quotewords(",", 0, $text);
There's also a Text::CSV (Comma-Separated Values) module on
CPAN.
my $string = '"cellone",celltwo,cellthr
$string =~ s/"(.*?)\,(.*?)"/$1::$2/g;
for (split(/\,/, $string)) {
s/\:\:/,/g;
print $_,"\n";
}
perldoc -q split
How can I split a [character] delimited string except when inside
[character]? (Comma-separated files)
Take the example case of trying to split a string that is
comma-separated into its different fields. (We'll pretend you
said comma-separated, not comma-delimited, which is different
and almost never what you mean.) You can't use "split(/,/)"
because you shouldn't split if the comma is inside quotes. For
example, take a data line like this:
SAR001,"","Cimetrix, Inc","Bob Smith","CAM",N,8,1,0,7,"Er
Due to the restriction of the quotes, this is a fairly complex
problem. Thankfully, we have Jeffrey Friedl, author of a
highly recommended book on regular expressions, to handle these
for us. He suggests (assuming your string is contained in
$text):
@new = ();
push(@new, $+) while $text =~ m{
"([^\"\\]*(?:\\.[^\"\\]*)*
| ([^,]+),?
| ,
}gx;
push(@new, undef) if substr($text,-1,1) eq ',';
If you want to represent quotation marks inside a quotation-
mark-delimited field, escape them with backslashes (eg, "like
\"this\"". Unescaping them is a task addressed earlier in this
section.
Alternatively, the Text::ParseWords module (part of the stan-
dard Perl distribution) lets you say:
use Text::ParseWords;
@new = quotewords(",", 0, $text);
There's also a Text::CSV (Comma-Separated Values) module on
CPAN.
yes but you can say that about anything. LOL..
Use 27 semi colons or 17 percent signs or something you are sure will not be in the object string naturally. No one solution will suffice for all possible data strings. Even the example for comma seperated files above can be broken by carefully crafted quotes.
Use 27 semi colons or 17 percent signs or something you are sure will not be in the object string naturally. No one solution will suffice for all possible data strings. Even the example for comma seperated files above can be broken by carefully crafted quotes.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
# Split a comma delimited string, where commas may appear inside quotes:
undef @field;
push(@field, defined($1) ? $1:$3) while
m/"([^"\\]*(\\.[^"\\]*)*)" |([^,]+)/g ;
undef @field;
push(@field, defined($1) ? $1:$3) while
m/"([^"\\]*(\\.[^"\\]*)*)"
my $string = 'cellone,celltwo,"cell has, comma",cell four';
$string =~ s/"(.*?)\,(.*?)"/$1::$2/g;
for (split(/\,/, $string)) {
s/\:\:/,/g;
print $_,"\n";
}