Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by philiprbrenan (Monk)
on Sep 01, 2012 at 11:33 UTC ( #991145=note: print w/ replies, xml ) Need Help??


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

Please consider building and manipulating the entire data structure in an array step by step, it makes it easier because you can see what you are doing with pp().

use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); use Text::CSV_XS; # So original data look something like: my @d = split /\n/, <<'END'; 1,6064.86,85391.25,593.75,13.25 2,6072.17,85392.95,593.79,13.29 3,6078.94,85393.05,593.76,13.26 4,6085.51,85392.22,593.77,13.27 END # I need to insert two lines at the top, two lines at the # bottom, switch columns 2 and 3 around and replace column 5 with colu +mn # 1.I'm using Text::CSV for parsing and also Lava GUI package. So part + of # the code that does parsing looks like this:D my ($t, @t) = (Text::CSV_XS->new); $t->parse($_) && push(@t, [($t->fields())[4,2,1,3,0]]) or die "Could n +ot parse: $_" for @d; # Parse and rearrange unshift @t, ["Top line 1"], ["Top line 2"]; # Top lines push @t, ["Bot line 1"], ["Bot line 2"]; # Bottom lines $t->combine(@$_) && ($_ = $t->string) or die "Cannot combine: ".dump( +$_) for @t; # Combine pp(\@t); # Print result

Produces

[ "\"Top line 1\"", "\"Top line 2\"", "13.25,85391.25,6064.86,593.75,1", "13.29,85392.95,6072.17,593.79,2", "13.26,85393.05,6078.94,593.76,3", "13.27,85392.22,6085.51,593.77,4", "\"Bot line 1\"", "\"Bot line 2\"", ]


Comment on Re: Parsing CSV only returns the second line of the file
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991145]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-12-28 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls