The whitespaces around the separator characters are not allowed in strict CSV. 
Try this below.

Cheers - Tobias

use strict;
use warnings;
use Text::CSV;

my $csv = Text::CSV->new({ allow_whitespace => 1 });
open my $fh, "<&DATA" or die "Can't access DATA: $!\n";
while (my $row = $csv->getline($fh)) {
        print join("\n",@$row),"\n";
}
$csv->eof or $csv->error_diag();

__END__
"col , 1"  ,  col___'2' ,  col-3, "col,4"

-----Original Message-----
From: perl-win32-users-boun...@listserv.activestate.com 
[mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of Barry 
Brevik
Sent: Wednesday, November 09, 2011 5:35 PM
To: perl Win32-users
Subject: Problem with regex

Below is some test code that will be used in a larger program.

What I am trying to do is process lines from a CSV file where some of the 
'cells' have commas embedded in the (see sample code below). I might have used 
text::CSV but as far as I can tell that module also can not deal with embedded 
commas.

In the code below I have a regular expression who's intent is to look for  " <1 
or more characters> , <1 or more characters> " and replace the comma with |. 
(the white space is just for clarity).

IAC, the regex works, that is, it matches, but it only replaces the final 
match. I have just re-read the camel book section on regexes and have tried 
many variations, but apparently I'm too close to it to see what must be a 
simple answer.

BTW, if you guys think I'm posting too often, please say so.

Barry Brevik
============================================
use strict;
use warnings;
 
my $csvLine = qq|  "col , 1"  ,  col___'2' ,  col-3, "col,4"|;
 
print "before comma substitution: $csvLine\n\n";
 
$csvLine =~ s/(\x22.+),(.+\x22)/$1|$2/s;
 
print "after comma substitution.: $csvLine\n\n";

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to