in reply to Writing CSV files

Seems like a somewhat strange requirement. Presumably the application is dumb as a box of hammers and is quoting any internal *char type. Essentially you need to remember if a given field was quoted or not which is typically information that CSV parsers discard. You really need a specialised split that returns array refs for the fields where the first element is the field data and the second the quote status. Here is the basic idea:

#!/usr/bin/perl -w use strict; while( my $data = <DATA> ) { chomp($data); next unless $data; my $fields = get_fields( $data ); $fields->[rand(3)]->[0] = 'newval'; my $str = remap_fields( $fields ); print "$str\n"; } sub get_fields { [ map{ s/^"|"$//g ? [ $_, 1 ] : [ $_, 0 ] } split /(?<=\S),(?=\S)/ +, $_[0] ]; } sub remap_fields { join ',', map{ $_->[1] ? qq!"$_->[0]"! : $_->[0] } @{$_[0]}; } __DATA__ "hi",3,20.6,"green","32" 16,"alpha",0.00

The get_fields() function may or may not work for you. The logic it uses not to split on embedded commas is to only split on \S,\S, This is making the invalid assumption that embeded commas probably look, like, this ie have spaces. You may have no embedded commas or the assumption may be true. If it is not you will need a better parser.

cheers

tachyon