in reply to Split Trailing Nulls

Most CSV-related posts end up with an obligatory reference to Text::CSV or a similar module, and a reminder about the CSV format. Here's mine:

If your data is really CSV data, don't forget to take into account the possibility of bizarre quoted constructs, such as
Field 1,"""This is quoted,"" and this isn't", Field 3

Splitting on a comma with real CSV data is unsafe. Consider a module such as the above link, or use another method of dividing the fields. Here's mine, which is ugly but workable:

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; nex +t; } } $array[$count] .= $char; } return @array; }