http://qs321.pair.com?node_id=1092419


in reply to Re^2: converting txt to csv and formatting
in thread converting txt to csv and formatting

Generating valid CSV records is more trivial than parsing CSV records. In this case, all of your data are character strings, not numbers or timestamps, so it's appropriate to quote all three fields all the time.

"Jacobs, April","750.467.9582","quam.quis@sedhendrerit.org" "Mays, Martena","870.348.1974","sollicitudin@nonummyFusce.org" "McNeil, Brennan","289.527.6151","lobortis@nisl.com" "Sexton, Melvin ""The Copymeister""","599.927.5337","in.felis@vari +us.com" "Blackburn, Prescott","661.589.1228","sed@egetlaoreetposuere.edu"

The most important thing to anticipate when generating CSV records in which every field is quoted is the possibility of the presence of the quote character in the data. The most common convention nowadays for escaping literal occurrences of the quote character is to use the same character as an escape character ("").

for (@client_values) { s/(?=")/"/g; s/^/"/; s/$/"/; } my $client_record = join ',', @client_values;

Don't print the record piecemeal, one field at a time. Doing this is a worst practice, IMHO. Instead, generate a valid, whole CSV record and then print it.

print "$client_record\n";