Melly has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code snippet:
foreach(@rec){
  if(/^(\d)(\d{5})(.*)$/){  #rec type, itemno, value
    $num = abs $2;
    $ival = $3;
    # Vrx handling
    if($1 == 1){  #vrx
      $vrx = abs($num . $ival);
    }
and I occasionally get the warning:
'Use of uninitialized value in numeric eq (==) at convert.pl line 240,'
Line 240 is, needless to say, the $1 == 1 line. If I add 'print $1' just above the line, I can see that it has a numeric value, so why and how (given the RE) am I getting this warning?
Any ideas?
Ta.
Tom Melly, tom@tomandlu.co.uk

Replies are listed 'Best First'.
Re: Odd warning
by Sidhekin (Priest) on Jul 25, 2002 at 16:17 UTC

    Any ideas?

    Well, if there is an elsif block following (that could cause the same warning), you had better look into it.

    ... perl occasionally gets confused WRT line numbers. At least those versions of perl that I've worked with. Would be nice if 5.8.0 had fixed this ... I haven't tried it yet :-)

    The Sidhekin
    print "Just another Perl ${\(trickster and hacker)},"

      BINGO! (the problem was in the elsif block)

      Many, many thanks

      Tom Melly, tom@tomandlu.co.uk

Re: Odd warning
by tadman (Prior) on Jul 25, 2002 at 16:05 UTC
    As a note, you're using abs, but the specification doesn't allow for minus signs. \d contains only digits 0-9 and nothing else. Maybe you want to widen your spec to ([0-9\-\.]{5}) or something similar.

    On some test data that I made up, I didn't get any warnings. Maybe you have something that would help, something more specific? As it is, it doesn't seem like you should be getting any warnings, but you may be using an antiquated perl which might not work like the 5.6.1 that I tested on.

      The abs $2 is to chop off any leading 0's (00012 to 12 for example). Probably not the approved method, butwhatthehell.

      Haven't really got any additional info. on the problem. I'm basically going through a large data file, and each line starts with a numeric. Everything goes fine, but then suddenly I'll get a crop of warnings, then fine again, then warnings.

      It doesn't really make any sense to me, and I wonder if the problem is elsewhere in my code and being misreported.

      Still, apart from that, I get no warnings, so I guess I'll turn 'em off.

      Oh, perl 5.6.0 under win2000.

      Tom Melly, tom@tomandlu.co.uk

        Melly wrote: The abs $2 is to chop off any leading 0's (00012 to 12 for example).

        If you run into this frequently, you can just add zero to the string to force a numeric context and eliminate those zeroes.

        perl -e '$foo="007";print "$foo\n";$foo+=0;print "$foo\n"' 007 7

        Cheers,
        Ovid

        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Odd warning
by dws (Chancellor) on Jul 25, 2002 at 16:27 UTC
    Weird. The code looks straightforward. If the regex matches, you should have a single digit in $1.

    Can you isolate (and post) a record in @rec that causes the warning? That'd give us a bit more to go on.