Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Need help with arrays and CSVs

by trey5498 (Initiate)
on Dec 26, 2007 at 19:17 UTC ( #659101=perlquestion: print w/replies, xml ) Need Help??

trey5498 has asked for the wisdom of the Perl Monks concerning the following question:

I am currently trying to learn Perl (never done until now) and I wrote a small peice of code that I want to read in from an CSV file and place it into an array. At this time I want to print each line of the array so that I know that I am doing it right. Later I will putting it into an output.csv with some changes made. Here is the code which doesnt seem to working right. PLEASE HELP!!!!!!!!!!!!

Code

use Text::CSV; my @printerstat; $file = 'inputdata.csv'; 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;

Replies are listed 'Best First'.
Re: Need help with arrays and CSVs
by ww (Archbishop) on Dec 26, 2007 at 20:35 UTC
    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"; }
      ...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
Re: Need help with arrays and CSVs
by jZed (Prior) on Dec 26, 2007 at 20:33 UTC
    From the docs for Text::CSV_XS which also apply to Text::CSV:
    my $csv = Text::CSV->new ({ binary => 1, eol => $/ }); open my $io, "<", $file or die "$file: $!"; while (my $row = $csv->getline ($io)) { my @fields = @$row; }
    P.S. Please use <code>...</code> tags so your code is more legible.
Re: Need help with arrays and CSVs
by derby (Abbot) on Dec 26, 2007 at 20:13 UTC

    Here is the code which doesnt seem to working right.

    Well ... what's not working right?

    -derby

    PS ... welcome to perl and the monastery ... check out How (Not) To Ask A Question.

Re: Need help with arrays and CSVs
by NetWallah (Canon) on Dec 26, 2007 at 20:27 UTC
    Please use code tags described in Writeup Formatting Tips.

    Your code puts all the parsed columns in the entire file into a single flat array, printing the array as it accumulates.

    Do you need to accumulate the entire file before processing it, or will you be working one line at a time ?

    If you let us know what you want the code to accomplish, and what error messages you are getting, perhaps we can assist you better.

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Re: Need help with arrays and CSVs
by jrsimmon (Hermit) on Dec 26, 2007 at 21:00 UTC
    Hello trey5498- Ok, so I cleaned this up a bit and added code tags. After I installed Text::CSV, this code ran perfectly well on my machine. It basically takes every comma separated value and pushes it onto the @printerstat array. Is that what you intended? If not, you'll need to be more specific about the goal your attempting to reach and the problem you're encountering.

    Please note the use of
    use strict; use warnings;
    at the beginning of the script. I've been casually coding in perl for a few years now and have found them to be invaluable.

    Also see PerlMonks FAQ, Writeup Formatting Tips, and How do I post a question effectively? to familiarize yourself with the surroundings.
    use strict; use warnings; use Text::CSV; my @printerstat = (); my $file = 'inputdata.csv'; 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;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2021-03-02 14:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (51 votes). Check out past polls.

    Notices?