Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^5: converting txt to csv and formatting

by Jim (Curate)
on Jul 06, 2014 at 02:47 UTC ( #1092424=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1092424]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2020-09-30 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (171 votes). Check out past polls.

    Notices?