Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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\"", ]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991145]
help
Chatterbox?
choroba still has one more.
[Corion]: I have Thursday and Friday off, so then I might get to converting the rough outlines to more articles ;)
[Corion]: But currently, most of the modules are web-related and I don't like to publish two web articles in a row
[Corion]: Maybe I should do the Filter::Simple release on the next weeked - this would give me one more article to milk from this theme

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2017-04-24 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (437 votes). Check out past polls.