in reply to CSV file

I didn't have time yesterday to put this idea into code, but since no-one else did it meanwhile, here it is. The crux of the OP is the need for a tiny enhancement to split, which is as simple as this:
sub MySplit2 { # args being like split but with the third INSTEAD a # preservation delimiter -- the OP requirement seems # to suggest that should be -1 for split anyway my $pat = shift; my $str = ( shift () || $_ ); my $pat2 = shift; # a preservation delimiter # i.e. not splits usual arg3 my @return = (); my $inquotes = 0; for my $qs ( defined ( $pat2 ) ? ( split( /$pat2/, $str, -1 ) ) : ( $str ) ) { push @return, $inquotes ? $qs : split( /$pat/, $qs, -1 ); $inquotes = !$inquotes; } return @return; }
But even if taken to the extreme of including all split's existing bells and whistles, it still manages to remain reasonable in size:
# example: # while (<FILE>) { # ( $name, $address, $tel ) = MySplit( ',',,,'"' ); # } sub MySplit { # a wrapper for split # args being like split but with a fourth for optional # preservation delimiter my $pat = shift; my $str = ( shift () || $_ ); my $cnt = shift; # copy of split's normal third argument # will be iterated down to zero here my $pat2 = shift; # the new argument my @return = (); my @topush; my $inquotes = 0; for my $qs ( defined ( $pat2 ) ? ( split( /$pat2/, $str, -1 ) ) : ( $str ) ) { $cnt or ( defined( $cnt ) and last ); # arg3>0 exhaustion @topush = $inquotes ? ($qs) : split( /$pat/, $qs, $cnt ); # update @topush and $cnt to agree with each other if ( defined( $cnt ) and ( $cnt >= 0 ) ) { $cnt -= ( $#topush + 1 ); if ( $cnt < 0 ) $#topush += $cnt; $cnt = 0; } } $inquotes = !$inquotes; push @return, @topush; } return @return; }

-M

Free your mind