Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Parsing Parsing CSV only returns the second line of the file

by Tux (Abbot)
on Sep 01, 2012 at 15:40 UTC ( #991167=note: print w/replies, xml ) Need Help??


in reply to Parsing CSV only returns the second line of the file

The outer loop should be written like, more modern and safe (think embedded newlines).

my $csv_in = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 }); my $csv_out = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, eol => + "\n" }); open my $fh_old, "+<", $file or die Lava::Message ("Can't open origina +l file: $file: $!"); open my $fh_new, ">", $temp or die Lava::Message ("Can't open tempfil +e file: $temp: $!"); $csv->getline ($fh_old); # skip first line. Header?. If empty line, 's +calar <$fh_old>;' is a good option while (my $row = $csv->getline ($fh_old)) { : : $csv_out->print ($fh_new, [ map { $row->[$_] } 0, 2, 1, 3, 0 ]); } $csv_out->print ($fh_new, [ 0, 0.000, 0.000, 0.000 ]); $csv_out->print ($fh_new, [ 0, 0.000, 0.000, 0.000, "END" ]); close $_ for $fh_old, $fh_new;

Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991167]
help
Chatterbox?
[Lady_Aleena]: $ grep --include=*.p[lm] -Ern 'get_(array|hash| data)' does not do multiline search.
[Discipulus]: focus on Perl LA; you can already search multiline patterns
[erix]: grep is a program, not a regex-engine
[Tanktalus]: perldigious: I'm 20 years in, and I don't want to move ;)
[Lady_Aleena]: erix, right, it searches files for a pattern. pcregrep is supposed to do the same thing with the addition of being able to search multiline.
[erix]: ah, pcregrep is a stanalone program?
[Lady_Aleena]: erix, yes.
[Lady_Aleena]: pcre stands for 'Perl Compatible Regular Expressions'.
[erix]: ok, ok, I thought you meant the engine
[perldigious]: Tanktalus: Ha, understandable, that does make it harder when you don't want to move. I assume you are looking at remote work from home jobs too?

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (13)
As of 2017-05-23 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?