Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^4: converting txt to csv and formatting

by csorrentini (Acolyte)
on Jul 06, 2014 at 02:34 UTC ( [id://1092421]=note: print w/replies, xml ) Need Help??


in reply to Re^3: 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? text file is not allowed to be editted, have to use it the way it is currently.
  • Comment on Re^4: converting txt to csv and formatting

Replies are listed 'Best First'.
Re^5: converting txt to csv and formatting
by Jim (Curate) on Jul 06, 2014 at 02:47 UTC
    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;
      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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1092421]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found