in reply to split on comma unless within quotes...

Hi bcarroll,

I am trying to parse a CSV file into an HTML table, but I am running into an issue where one of the fields contains commas. I am thinking I may need to use a different regular expression in the split statement, but wanted to check to see if anyone had a better approach.

To parse CSV file, you will want to use Text::CSV instead of parsing by hand using split function. Though that "may" do on some occasions but it's alot better using a tested module.

Though you didn't show how you want the output look like and which field that you are still having comma in. Using your dataset and going by the title of your post something like this works:

use warnings; use strict; use Text::CSV; my $csv = Text::CSV->new( { binary => 1 } ) or die Text::CSV->error_diag(); print "<Table border = 1 cellpadding = 1 callspacing = 1 width = 80% align = + 'center'>"; while ( my $row = $csv->getline( \*DATA ) ) { print "<tr>"; print "<td>$_</td>" for @$row; print "</tr>"; } print "</Table>"; __DATA__ Source,Destination,User,State 192.168.0.2,192.168.0.6,"cn=user1,ou=infrastructure,ou=accounts,o=ORG, +c=US",Allowed 192.168.0.3,192.168.0.6,"cn=user2,ou=infrastructure,ou=accounts,o=ORG, +c=US",Denied
OUTPUT
SourceDestinationUserState
192.168.0.2192.168.0.6cn=user1,ou=infrastructure,ou=accounts,o=ORG,c=USAllowed
192.168.0.3192.168.0.6cn=user2,ou=infrastructure,ou=accounts,o=ORG,c=USDenied

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me