Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: converting txt to csv and formatting

by AppleFritter (Vicar)
on Jul 06, 2014 at 00:11 UTC ( [id://1092409]=note: print w/replies, xml ) Need Help??


in reply to converting txt to csv and formatting

You're guaranteed to have three lines of information per client entry, right? You could do this (not the prettiest, perhaps):

while(my $client = <>) { chomp $client; chomp(my $phone = <>); chomp(my $email = <>); ... }

For writing your CSV file, I'd suggest using a module from CPAN (e.g. Text::CSV) rather than rolling your own.

Replies are listed 'Best First'.
Re^2: converting txt to csv and formatting
by csorrentini (Acolyte) on Jul 06, 2014 at 01:07 UTC

    Thanks for the quick reply, Yes guaranteed three lines of information always.

    As far as writing to the csv this is a class assignment and the instructor said "You really don’t need any external modules (like the CSV module we used in an earlier script)." so I don't believe he wants us to use it for some strange reason. I input your piece of coding into the script, now without using Text::CSV is it just a simple print to the csv file?

      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";
        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.

      If I might paraphrase someone else, there are no guarantees except that there are no guarantees when it comes to data formats. Just remember that the RS232 standard isn't. :)

      You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-19 18:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found