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


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

This would be assuming that I can modify the text file?

Uh… no. What makes you think that?

This is just a snippet of the code you'd use within your line-reading while loop.

BTW, you could do the chomping in the same for loop as the rest of the formatting.

for (@client_values) { chomp; s/(?=")/"/g; s/^/"/; s/$/"/; }

Alternatively…

for (@client_values) { s/(?=")/"/g; s/^/"/; s/\n$/"/; }

Update:  Maybe you were confused by my use of an array variable instead of three scalar variables. Let's assume you've read three lines (i.e., three client data values) into three variables as AppleFritter demonstrated here, but without chomping them as you read them.

for ($client_name, $client_phone_number, $client_email_address) { s/(?=")/"/g; # Escape quote characters... s/^/"/; s/\n$/"/; # chomp and append quote character simultaneously +... } my $client_record = join ',', $client_name, $client_phone_number, $client_email_ +address; print "$client_record\n";

Another update:  It occurs to me now that if you're new to Perl, then you're probably not familiar with the fancy regular expression pattern in this global substitution operation:

s/(?=")/"/g;

It's a look-ahead assertion, which is sort of an intermediate Perl topic. (See perldoc perlre.) If it's easier to understand, then just do this instead:

s/"/""/g;

Replies are listed 'Best First'.
Re^6: converting txt to csv and formatting
by csorrentini (Acolyte) on Jul 06, 2014 at 04:09 UTC
    Yes, sorry still VERRRY new to perl so alot of this is still foreign to me. I'll try to incorporate that and see how it goes. Thank you so much for the help i really appreciate it.