#!/usr/bin/perl use strict; sub csv_split { local $_ = shift || return undef; my @array = (); my $count = my $quoted = 0; while ( s/(.)// ) { my $char = $1; if ($char eq ',' && !$quoted) { $count++; next; } if ($char eq q/"/) { unless ( $quoted && s/^\"// ) { $quoted = 1 - $quoted; next; } } $array[$count] .= $char; } return @array; } for my $line () { print "\nLine: $line"; print "$_\n" for csv_split $line; } __DATA__ Simple cells,test,test Cells with commas,"test 1a,1b","test, 2a 2b" Cells with quotes,"test ""one""","""test"" two" Quotes and commas,"test ""1a,1b""","""test 2a,""2b"