Oh great MonksWhoKnowStuffIHaveNotYetFiguredOut, this is probably really basic, but it keeps coming up and poking me and I can't seem to find a decent solution. As an example, the following code:
use strict; use warnings; my @data=( [1,2,3], [4,5,6], [7,8], [10,11,12] ); my @categories=qw/a b c/; my @master; foreach my $d(@data){ my %h; foreach my $c(0 .. $#categories){ my $val=$d->[$c]; my $cat=$categories[$c]; $h{$val}=$cat; } push @master, \%h; }
produces this error:
Use of uninitialized value $val in hash element at ./test.pl line 23.
Now, this is certainly a trivial case, but it keeps happening to me over very large lists of data that are read in from a file. Typically there's a typo in the data of some kind.
What I end up having to do is build a bunch of code to trap that specific error, based on a guess as to what caused it, and possibly in conjunction with printing each statement to STDERR along with a line number. Doing this on a semi-regular basis causes me to think unpure thoughts, and is probably having a negative effect on my Eternal Karma.
What I would like is an easy way to know the line number in the file (or the array index I've slurped it into), and the actual incoming data that annoyed Perl, so I can fix it in the data file. Knowing the problem line in the code is only partially useful when it's a data problem.
An appropriate liquid sacrifice to the monastary will be made in an appropriate manner later, but may be upgraded based on the quality of answers.
In reply to Errors on loops over data by noop
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |