Thanks for the update, now it's clear why Perl doesn't automagically know the input file information: You're reading the entire file into @lines, and by the time you write to output.csv, input.csv is already closed.
The quickest fix would be to implement the %SIG __WARN__ handler as described here. Here's a quick example (my input.txt contains just four lines, "foo", "bar", "quz" and "baz"):
open my $infh, '<', 'input.txt' or die $!; my @lines = <$infh>; close $infh; # open output file here my $i; local $SIG{__WARN__} = sub { my $msg = shift; $msg =~ s/\.?\s*$//; warn "$msg (input line $i)\n"; }; for($i=0;$i<@lines;$i++) { # do calculations and write to output file here # example warning: warn "found a z" if $lines[$i]=~/z/; } __END__ # Script Output: found a z at - line 14 (input line 2) found a z at - line 14 (input line 3)
Note that the "line numbers" reported here are 0-based, as it's actually an index into the array of lines. But the advantage is that here, the message is fully customizable, so you're free to print $i+1 or whatever else you like.
However, unless you've oversimplified your example, your code could be made more efficient if it were to keep the input file open while writing the output file. Also, as long as the input file stays open, Perl will normally add the input file line numbers to warning messages by itself. So for example:
open my $infh, '<', 'input.txt' or die $!; # open output file here while( my $line = <$infh> ) { # do calculations and write to output file here # example warning: warn "found a z" if $line=~/z/; } close $infh; __END__ # Script Output: found a z at - line 6, <$infh> line 3. found a z at - line 6, <$infh> line 4.
By the way, your code would be better if you used the three-argument open and error handling (or die), as in the above examples. Also, you really should look into Text::CSV for better handling of CSV files!
In reply to Re: Find data point generating Error in Perl code
by Anonymous Monk
in thread Find data point generating Error in Perl code
by kgherman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |