CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
SAving data to a CSV file

 



gouse.aman
Novice

May 2, 2013, 2:02 AM

Post #1 of 8 (644 views)
SAving data to a CSV file Can't Post

Hi,
I want to save my output data which is tab separated to a csv file. Let me know the best option available in perl.Smile


simbu_007
New User

May 2, 2013, 3:32 AM

Post #2 of 8 (639 views)
Re: [gouse.aman] SAving data to a CSV file [In reply to] Can't Post

hi,

Can you try below method for save csv file.

open(CSV, ">D:\check.csv");
print CSV "NAME\tSEX\tAGE\n";
print CSV "Anbu\tMale\t20\n";
print CSV "Akilan\tMale\t22\n";
close(CSV);


gouse.aman
Novice

May 2, 2013, 4:52 AM

Post #3 of 8 (634 views)
Re: [simbu_007] SAving data to a CSV file [In reply to] Can't Post

Thanks a lot simbu for the response!
It gave me nice idea to implement my own way.WinkWink
It's below:

#!/usr/bin/perl -w

open (FILE, ">commasep.csv");
print FILE "NAME,SEX,AGE\nGOUSE,MALE,23\nAMAN,MALE,23\n";
close FILE;


(This post was edited by gouse.aman on May 2, 2013, 8:51 PM)


recruiter
User

May 2, 2013, 6:59 PM

Post #4 of 8 (609 views)
Re: [gouse.aman] SAving data to a CSV file [In reply to] Can't Post

Please consider being more explanatory next time. By data do you mean just data you are supplying or data from another text/file that you want to save to CSV? Also the user asked for a better option in perl, by no means I am saying your code is wrong, but I would just address what is asked more politely.


Code
   

open(CSV, ">D:\check.csv");
print CSV "NAME\tSEX\tAGE\n";
print CSV "Anbu\tMale\t20\n";
print CSV "Akilan\tMale\t22\n";
close(CSV);

I would write this as so if you just want to use a simple save.

open my $fh, '>', 'check.csv' or die $!;
print $fh "name\tsex\tage\n";
close $fh;



You are probably wondering what is my? Well my creates a new lexical variable. The variable only exists until the innermost enclosing block of code is exited. To start I would read perl documentation on lexical variables for more of a explanation.

For a more simple and better method I would suggest using CPAN's module Text::CSV.

Here is an example of how you could do this:


Code
 
use strict;
use warnings;

use Text::CSV;

my @rows = qw(name age sex);

my $csv = Text::CSV->new ( { binary => 1, eol => "\n" } )
or die "error: ".Text::CSV->error_diag ();

open my $fh, '>', 'check.csv' or die $!;
$csv->print( $fh, $_ ) for @rows;
close $fh;



(This post was edited by hwnd on May 2, 2013, 7:19 PM)


gouse.aman
Novice

May 2, 2013, 8:40 PM

Post #5 of 8 (603 views)
Re: [hwnd] SAving data to a CSV file [In reply to] Can't Post

Hi hwnd,

I mentioned it's an out so it does mean from some source and I mentioned the type of data is tab separated. I suppose I have mention it well enough. Meanwhile I found the solution.
Thanks for the concern.


FishMonger
Veteran / Moderator

May 3, 2013, 6:14 AM

Post #6 of 8 (587 views)
Re: [gouse.aman] SAving data to a CSV file [In reply to] Can't Post


Quote
Let me know the best option available in perl.


Your chosen solution is far from the best. It's closer to the other end of the scale.


gouse.aman
Novice

May 5, 2013, 10:47 PM

Post #7 of 8 (566 views)
Re: [FishMonger] SAving data to a CSV file [In reply to] Can't Post

Seems like this forum very much meant to pull out one-another's leg and point only mistakes in the queries rather than to save out someone out of ordeal!!

Thanks and I would quit this forum.


BillKSmith
Veteran

May 6, 2013, 6:31 AM

Post #8 of 8 (557 views)
Re: [gouse.aman] SAving data to a CSV file [In reply to] Can't Post

I am sorry that you feel this way. There probably is no objective "best option". Therefore, you are asking for opinion. Disagreement is probably healthy. You have already received several options with reasons for and against. I believe that each responder intends to help you make the right choice, not to make it for you.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives