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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.