Regarding LanX's suggestion: Remove Tabs and Newlines Inside Fields of Text Tab Delimited Files from Excel.
Untested for comma delimiters, but presumably you would just have to change all the split and join lines to use commas instead of tabs.
Also, from another post: Do I have to trick Split? Using "-1" for the third split parameter fixed some warnings trailing empty data columns caused during the subsequent join.
my @data = resolve_comma_delimited_file_line($CSVFILE, $this_line); # This subroutine accepts a filehandle and a line read from that fileh +andle as arguments given in that order. # If necessary it will modify the line that was passed to it (as if pa +ssed by reference) to resolve it, and return an array of the split da +ta. sub resolve_comma_delimited_file_line { my $fh = $_[0]; chomp($_[1]); # $_[1] being the read line passed in to this subrou +tine that is to be modified if necessary (as if passed by reference) my @data = split /,/, $_[1], -1; my $last_index = $#data; for (my $field_index=0; $field_index<$last_index; $field_index++) { if (($data[$field_index] =~ tr/"//) % 2 == 1) { splice @data, $field_index, 2, "$data[$field_index] $data[ +$field_index+1]"; $_[1] = join ",", @data; $last_index--; $field_index--; } } if (($data[$last_index] =~ tr/"//) % 2 == 1) { $_[1] .= " " . <$fh>; @data = &resolve_comma_delimited_file_line; } return @data; }
UPDATE: Deleted comments related to tabs since they wouldn't be relevant for the comma case.
That being said, Text::CSV is still probably a better option because it saves you from having to "reinvent the wheel" so to speak, and probably is a lot more robust solution for the types of hiccups you may encounter in your file data.
In reply to Re^3: Embedded Newlines or Converting One-Liner to Loop
by perldigious
in thread Embedded Newlines or Converting One-Liner to Loop
by mwb613
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |