in reply to Re: Find data point generating Error in Perl code
in thread Find data point generating Error in Perl code

Thank you, thank you, thank you! Your post is a goldmine of valuable information.

I have followed your suggestion and now I keep the input file handle open while writing the output. As you had guessed, this allowed me to have the number of the line in the input file added to the warning message! thank you!

I will definitely look into the Text::CSV module since I only work with CSV files. Also, I will implement the three-argument open and error handling notation.

I have one last question: I was looking at the last code that you have written in your answer and I was wondering: does your notation mean that you read one line at the time from the CSV file or, like in mine, do you load the entire file in memory and then work on it?

  • Comment on Re^2: Find data point generating Error in Perl code

Replies are listed 'Best First'.
Re^3: Find data point generating Error in Perl code
by Anonymous Monk on Mar 17, 2015 at 21:25 UTC

    A while loop over a filehandle like while(my $line = <$fh>) { ... } reads the file line-by-line* (one line is read on each iteration of the loop), as opposed to foreach my $line (<$fh>) { ... } or my @lines = <$fh>;, which reads the entire file first (obviously not particularly friendly on memory for large inputs).

    For more information, see I/O Operators in perlop and readline.

    * Perl's definition of what a "line" is may be changed via the input record separator $/.

    ... since I only work with CSV files.

    Text::CSV!

    I will definitely look into the Text::CSV module...

    Yes! ;-)

      I have decided to change the way my code reads in the data file to make it the way you suggested and it has definitely helped a lot!

      However, using your way, I'm still trying to figure out how to make perl skip the first line in the data file since it contains the names of the variables... what do I need to change in the while loop? thanks!

        However, using your way, I'm still trying to figure out how to make perl skip the first line in the data file since it contains the names of the variables... what do I need to change in the while loop? thanks!

        One way is to put my $header = <$fh>; just before the while loop (replace $fh with whatever you've called your file handle). <$fh> in scalar context will read a single line from the file. You don't need to do anything with $header, I just find the code more readable if you label what you're doing.