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

Re: Need help with arrays and CSVs

by ww (Archbishop)
on Dec 26, 2007 at 20:35 UTC ( #659113=note: print w/replies, xml ) Need Help??


in reply to Need help with arrays and CSVs

OP's code, for ease of reading, with a note re the missing "my" and strict and warnings added:
#!C:/perl/bin use warnings; use strict; use Text::CSV; my @printerstat; $file = 'inputdata.csv'; # should be <b>my</b> $file my $csv = Text::CSV->new(); open (FILE, $file) or die "Couldn't open location file: $!"; while (<FILE>) { $csv->parse($_); push(@printerstat, $csv->fields); print @printerstat; } close FILE;
when run against a csv:

this is a field to start with,field2,field3

this produces what I would expect, namely:

this is a field to start with.field2field3

On the supposition that "I want to print each line of the array" means you want each field in the csv printed on a separate line replace
print @printerstat;
with:
for my $field(@printerstat) { print "$field\n"; }

Replies are listed 'Best First'.
Re^2: Need help with arrays and CSVs
by ww (Archbishop) on Dec 26, 2007 at 21:02 UTC
    ...and if your data is multiline, eg:
    this is a field to start with,field2,field3 #,*,field3 of line 2 field1 of line3,and this is the second field of line3,field3 of line3

    then replacing the relevant part of the above code with this fragment produces a line of output for each field:

    while (<FILE>) { $csv->parse($_); push(@printerstat, $csv->fields); # print @printerstat; } spit(@printerstat); sub spit { for my $field(@printerstat) { print "$field\n"; } }

    thusly

    this is a field to start with field2 field3 # * field3 of line 2 field1 of line3 and this is the second field of line3 field3 of line3

    This also works if some of your fields are void, eg:

    ,,this is a row with two empty fields

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2021-04-19 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?