Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

generating csv file

by Anonymous Monk
on Feb 02, 2012 at 21:27 UTC ( #951536=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have been using the Text::CSV module to read csv files and now come to the part where I need to generate a revised csv file, and am confused how to do that.

while ( my $row = $csv->getline( $fh ) ) { $patron_count++; push @rows, $row;

I am fine if I want to keep the whole row:

$csv->print ($fh, $_) for @rows;

but I am confused when I need to modify the '@rows', ie remove some of the columns(fields) or add some new fields on to it.

I am sorry for the basic question, but I am doing some volunteer work for a theatre trying to move from one system to another, and after being away from coding for 8 years, I have forgotten it all!

I think I understand that '@rows' just isn't an array, as I have been accessing the columns by the syntax of $row->2, but now I don't know how to create the structure that the csv writer needs. It wants an array ref and I am lost!

The three things I need to do are:

1. Only use (out of 20 cols, columns 1-10,12,15).

2. Modify the value of col 1 by prefixing the character 'r'

3. Add a new column to each row as i parse it, with a string value for its content.

If I could get help so i can proceed, I would be very thankful. I have read and reread the perl docs on the module but am still lost.

Replies are listed 'Best First'.
Re: generating csv file
by wwe (Friar) on Feb 03, 2012 at 10:37 UTC
    Hi, the most interesting thing you may be missing all data you get from Text::CSV using $row = $csv->getline( $fh ) statement is an array refence and the following statement push @rows, $row; puts this array reference to an array so you end with a data structure called array of array (AoA). You can change the data while it gets parsed as shown in the docs of the Text::CSV or later when it's inside of AoA @rows. Here is a sample script for change data while it's read:
    # ..\perl.exe # perlmonks # Text::CSV witer use strict; use warnings; use Text::CSV; my $input_file = "951536_in.csv"; my $output_file = "951536_out.csv"; my @rows; my $csv = Text::CSV->new ( { binary => 1 } ) # should set binary att +ribute. or die "Cannot use CSV: ".Text::CSV->error_diag (); #read file open my $fh, "<:encoding(utf8)", $input_file or die "$input_file: $!" +; while ( my $row = $csv->getline( $fh )) { $row->[0] =~ s/^(.*)(\d+)$/abc$2/; # replace leading chars $row->[1] =~ s/^(.*)(\d+)$/$1asd$2/; # insert between char and di +git $row->[2] =~ s/^(.*)(\d+)$/$1xyz/; # replace digits push @rows, $row; } $csv->eof or $csv->error_diag(); close $fh; #write file $csv->eol ("\n"); open my $fh_out, ">:encoding(utf8)", $output_file or die "$output_file +: $!"; $csv->print ($fh_out, $_) for @rows; close $fh_out or die "$output_file: $!"; print "finish!\n";
    you also need this file 951536_in.csv:
    a1,a2,a3 b1,b2,b3 c1,c2,c3
    and the output should be like this 951536_out.csv:
    abc1,aasd2,axyz abc1,basd2,bxyz abc1,casd2,cxyz

      Thank you, that helps for part of it. I was confused on what was the array ref. Now I can go study up on array of array so I can figure out how to 'make' my own, as I also have to remove some of the columns, not just modify in place.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://951536]
Approved by davies
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2017-09-25 15:21 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (280 votes). Check out past polls.