Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Reusing a Parse::CSV module

by adambe (Initiate)
on Dec 01, 2011 at 16:14 UTC ( #941110=perlquestion: print w/ replies, xml ) Need Help??
adambe has asked for the wisdom of the Perl Monks concerning the following question:

Problem was with some filenames in Windows which were causing trouble... Not_a_Number, I've added the "or die" clause, hopefully it will catch such problematic file names. Thanks, Adam. I've been playing around with Parse::CSV for a simple case of comparing two files – meaning I'd like to re-use the instance of the module. The second instantiation of the object results in an empty ARRAY reference… Am I missing something obvious or is there another way of going around this?
sub get_line { my ($file, $row_num) = @_; my $parser = Parse::CSV->new( file => $file, ); while ( my $value = $parser->fetch ) { if ($parser->row() == $row_num) { return $value; } ....
Thanks, Adam.

Comment on Reusing a Parse::CSV module
Download Code
Re: Reusing a Parse::CSV module
by RichardK (Priest) on Dec 01, 2011 at 17:22 UTC

    I have a few questions for you :-

    • What do you mean by "re-use the instance of the module" ?
    • How does that code sample relate to your problem ?
    • What does fetch return when there are no more lines left ?

Re: Reusing a Parse::CSV module
by Not_a_Number (Parson) on Dec 01, 2011 at 17:45 UTC

    Presuming that you just pass two filenames, one after the other, to your sub, your code works for me as is.

    But I don't see any error checking. Maybe the scond file doesn't exist, or you've misspelt its name?

    Change the second line in your sub to:

    my $parser = Parse::CSV->new( file => $file ) or die Parse::CSV->errstr;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2014-09-22 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (182 votes), past polls