Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: converting txt to csv and formatting

by 2teez (Vicar)
on Jul 06, 2014 at 01:23 UTC ( [id://1092415]=note: print w/replies, xml ) Need Help??


in reply to converting txt to csv and formatting

Hi,

..My problem is I have no idea how I would go about grabbing data into specific fields with the way the txt file is currently formatted...

I really don't think that is such a problem. With the data set you provided, one can do like this:

use warnings; use strict; print join ("\t\t" => qw(Name Phone E-mail)),$/; while (<DATA>) { chomp; print $_, "\t"; print $/ if $. % 3 == 0; # note here } __DATA__ 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 599.927.5337 in.felis@varius.com Blackburn, Prescott 661.589.1228 sed@egetlaoreetposuere.edu
Output
Name Phone E-mail 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 599.927.5337 in.felis@varius.com Blackburn, Prescott 661.589.1228 sed@egetlaoreetposuere.edu
The CSV part ( since you have mentioned that you can't use an external module ) and format to taste of the question is left for the OP.

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

Replies are listed 'Best First'.
Re^2: converting txt to csv and formatting
by csorrentini (Acolyte) on Jul 06, 2014 at 01:49 UTC
    That would be perfect formatting just need to get it onto a csv file and read from the txt vs writing it. Changed it to read from the text file now I just need to have it print to the csv.
    use warnings; use strict; my $infile="clients.txt"; open my $in, "<", $infile or die $!; open my $out, ">" , "clients.csv" or die $!; my $clients = print join ("\t\t" => qw(Name Phone E-mail)),$/; while (<$in>) { chomp; print $_, "\t"; print $/ if $. % 3 == 0; # note here }

      ..just need to get it onto a csv file and read from the txt vs writing it. Changed it to read from the text file now I just need to have it print to the csv...

      That is exactly what you will have to do, at least I have shown you that it's not impossible to get each client data on a single row.

      And what do you hope to achieve with this line in your code?: my $clients = print join ("\t\t" => qw(Name Phone E-mail)),$/;

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me

        I was trying to figure out how to print it to the output file and was hoping that would of made it easy but I was wrong. I modified it to this now:

        use warnings; use strict; my $infile="clients.txt"; open my $in, "<", $infile or die $!; open FH, ">" , "clients.csv" or die $!; print FH join ("," => qw(Name Phone E-mail)),$/; while (<$in>) { chomp; print FH $_, "\t"; print FH $/ if $. % 3 == 0; # note here }

        This gets the information into the csv file however I still need to modify the formatting, probably with a printf? What currently happens with this script is it creates the three columns "Name, Phone, Email" but the Name field is just the first name of the people while the Phone field gets EVERYTHING else. Would a simple printf or something solve this issue?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-28 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found