Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^3: Text to CSV with user input comma

by hdb (Monsignor)
on Jun 28, 2013 at 07:59 UTC ( #1041170=note: print w/replies, xml ) Need Help??

in reply to Re^2: Text to CSV with user input comma
in thread Text to CSV with user input comma

Would you mind being more specific? Can you tell us which module you are using and show a few lines of the code where you use it?

  • Comment on Re^3: Text to CSV with user input comma

Replies are listed 'Best First'.
Re^4: Text to CSV with user input comma
by shushant (Novice) on Jun 28, 2013 at 08:10 UTC
    #!/usr/bin/env perl use Text::CSV; my $csv = Text::CSV->new(); open(my $in_fh, "<", "") or die "please create your temporary file: $!"; my @rows = (); + # keeps the count of rows in txt input file while( my $line = <$in_fh> ){ + # copies the line from our input chomp $line; push @rows, [ split m{ , }msx, $line ]; + } close $in_fh or die "could not close $in_file: $!\n"; $csv->eol( "\n" ); + # after each line it gives a new line command print " Please enter the name of your csv file in which you will make +modifications "; print "\n"; my $out_file = <>; chomp $out_file; open my $out_fh, '>', $out_file or die "could not open $out_file: $!\n +"; for my $row ( @rows ){ $csv->print( $out_fh, $row ) or die "could not print to $out_file: $ +!\n"; } close $out_fh or die "could not close $out_file: $!\n";
    This is what I am doing

      You are using the module only for the generation of CSV, not for the parsing. Start by writing your parsing code safer:

      use Text::CSV; my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1 }); open my $in_fh, "<", "" or die "please create your temporary file: $!"; my @rows = (); while (my $row = $csv->getline ($in_fh)) { push @rows, $row; }

      I am sure I posted the same answer in this thread …

      Enjoy, Have FUN! H.Merijn
        Hey thanks for helping

        but i made the changes , but still the problem continues

      So you are reading from a file, split on comma, then write it into another file. I would expect that there are no commas in the data you are writing to the new file as all commas are gone due to the splitting.

      Probably some example would be useful like an input line and the corresponding output line that has double quotes in front of every word.

        yes pretty much , but what I have done is replaced the commas of text file to a delimiter as ;; , so that i can split it using ;; , and even if there is a comma in file it would not affect it

        input looks like 50;; C,nt ;; ;;8;; 1;; ds;; 1;; a;; 1;; 0;; 0;; ;; ;; ;; output looks like " 50"," C ,nt "," ",8," 1","ds","1"," Va"," 1"," 0"," 0"," "," "," " and I just want these " " to get omitted

         push @rows, [ split m{ ;; }msx, $line ];   I have done this in the code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1041170]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2017-08-21 07:36 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (318 votes). Check out past polls.