Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Using Text::CSV

by Win (Novice)
on Sep 24, 2009 at 12:27 UTC ( [id://797185]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

Your patience is never ending and appreciated. I am looking to use Text::CSV to read through a CSV file and present all rows where the 9th column meets a string condition. I believe that I need to use the following:

$colref = $csv->getline ($io); # Read a line from file $io # parse it and return an array # ref of fields

However there is a number of options that are confusing me. For example:
$status = $csv->parse($line); @columns = $csv->fields(); # get the parsed fields

Replies are listed 'Best First'.
Re: Using Text::CSV
by Tux (Canon) on Sep 24, 2009 at 12:34 UTC

    If reading from a stream, please forget about parse () and fields (). The two are combined a lot faster and more efficient into getline () when working in streams.

    open my $fh, "<", "file.csv" or die "file.csv: $!\n"; while (my $row = $csv->getline ($fh)) { $row->[8] =~ m/criterium/ or next; # do something with this line } $csv->eof or $csv->error_diag ();

    Enjoy, Have FUN! H.Merijn
      I think that snippet (and one for ouput) should be added to the top of the Synopsis, if not replace the current content of the Synopsis.

        The example is already in the docs since ages, and has been moved up to be the first in this diff in May. I don't know if that section could be even more clear than it already is.

        Note however that this is the documentation of Text::CSV_XS, and that this EXAMPLES section is not in the docs of Text::CSV.


        Enjoy, Have FUN! H.Merijn
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Using Text::CSV
by Anonymous Monk on Sep 24, 2009 at 14:34 UTC
    I don't understand why this is confusing, what are you reading?

    getline

    ...It reads a row from the IO object $io using $io->getline () and parses this row into an array ref. This array ref is returned by the function or undef for failure.
    parse
    This object function decomposes a CSV string into fields, returning success or failure. Failure can result from a lack of argument or the given CSV string is improperly formatted. Upon success, fields () can be called to retrieve the decomposed fields . Upon failure, the value returned by fields () is undefined and error_input () can be called to retrieve the invalid argument
Re: Using Text::CSV
by liverpole (Monsignor) on Sep 25, 2009 at 16:01 UTC
    Win, I never used Text::CSV before, but it can't be that hard, right?

    It installed fine for me just now using ppm:

    C:\Documents and Settings\jnorton>ppm install Text::CSV Downloading ActiveState Package Repository packlist...not modified Downloading Text-CSV-1.13...done Unpacking Text-CSV-1.13...done Generating HTML for Text-CSV-1.13...done Updating files in site area...done 4 files installed

    Even though your original post provides only a single line:

    $colref = $csv->getline ($io);

    (which you really cannot expect to be enough information, can you?!!), at least you provide an almost complete example by your 3rd post.  (I say "almost", because it still doesn't tell us what's in your data file "gridall.csv".  But no matter, we'll figure that out).

    So let's try running your program.  The only changes I've made are removing some blank lines, consolodating the text in the pring statements, and adding use strict; and use warnings; (which you have been told about many times before, but still seem oblivous to the purpose for -- just because you can't see the errors in your program doesn't mean they're not there!):


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Using Text::CSV
by Anonymous Monk on Sep 25, 2009 at 16:38 UTC
    Are you sure you're pointing at the right ppm repository? You might have to update your ppm config.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2025-07-10 20:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.