BlueStarry has asked for the wisdom of the Perl Monks concerning the following question:
Hi i want you to know that this question is crossposted on stackoverflow for more visibility thank ou for your kind understanding.
on the synopsis of Text::CSV there's this code:I see that it pushes some rows on the array @rows, and then the array gets printed line by line inside the original file. I need to do a similar thing (like checking for a pattern and then save the row to rewrite it on another file later) but i don't need all the row, i just need a field and i need to add new fields to the $row that is been pushed.. how would i do that.my @rows; my $csv = Text::CSV->new ( { binary => 1 } ) # should set binary att +ribute. or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, "<:encoding(utf8)", "test.csv" or die "test.csv: $!"; while ( my $row = $csv->getline( $fh ) ) { $row->[2] =~ m/pattern/ or next; # 3rd field should match push @rows, $row; } $csv->eof or $csv->error_diag(); close $fh; $csv->eol ("\r\n"); open $fh, ">:encoding(utf8)", "new.csv" or die "new.csv: $!"; $csv->print ($fh, $_) for @rows; close $fh or die "new.csv: $!";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Text::CSV module, writing some data on a csv.
by Corion (Patriarch) on Apr 06, 2016 at 10:28 UTC | |
by BlueStarry (Novice) on Apr 06, 2016 at 10:36 UTC | |
by hippo (Archbishop) on Apr 06, 2016 at 10:47 UTC | |
by BlueStarry (Novice) on Apr 06, 2016 at 11:02 UTC | |
|
Re: Text::CSV module, writing some data on a csv.
by Lotus1 (Vicar) on Apr 06, 2016 at 15:43 UTC |