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

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! ;-)

Replies are listed 'Best First'.
Re^4: Find data point generating Error in Perl code
by kgherman (Novice) on Mar 19, 2015 at 18:06 UTC
    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.

        Thanks, that did the trick and I definitely agree that labeling will make it more readable :)