Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

use of unitialized value

by madaket (Novice)
on Oct 20, 2003 at 18:02 UTC ( [id://300684]=perlquestion: print w/replies, xml ) Need Help??

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

I have a hash of lists (called inventory) that contain an itemcode (the key), description, price, and quantity.

When I try to find out if the quantity is greater than zero:

foreach my $itemcode (keys %inventory) { my $quantity = $inventory{$itemcode}[2]; if ( $quantity > 0 )
I always get this error message:
Use of uninitialized value in numeric gt (>) at (the if statement line).

Right now, I have all of the use strict; statements commented out.

Any suggestions on how to correct this error?

Thanks.

Replies are listed 'Best First'.
Re: use of unitialized value
by robartes (Priest) on Oct 20, 2003 at 18:10 UTC
    Try dumping the contents of $inventory{$itemcode} to see what's actually in it (using Data::Dumper:
    print Dumper($inventory{$itemcode}); # or even: print Dumper(\%inventory);
    That way, you can verify that the hash values contain what you think they contain.

    CU
    Robartes-

      Hmmmm...
      The funny thing about this error message is that it happens whether there is a value in

      $inventory{$itemcode}[2]
      or not. The loop will generate the error in each iteration.
      The value is from a web page form input, so I thought the problem might be a string/number conversion, but the error message doesn't indicate that.

Re: use of unitialized value
by ptkdb (Monk) on Oct 20, 2003 at 19:04 UTC
    First suggestion(not having seen any more of your code), turn 'use strict' back on. Then painstakingly correct everything it points out. "Use of uninitialized value" is a -w warning, but typically not a fatal error(depending on your point of view of course).

    You could use the 'ptkdb' debugger and set a breakpoint at your 'if' statement and set its condition do "!defined $quanity". The debugger will stop the program when that statement would generate that warning. That will allow you to see the 'offending member' in your hash. You can use the default perl debugger to do the same thing with the command: 'b <lineno> !defined $quantity'.

    Check the code that populates that hash.

Re: use of unitialized value
by dragonchild (Archbishop) on Oct 20, 2003 at 18:17 UTC
    Your problem is that you didn't put anything in that spot. I'd do a Data::Dumper on %inventory to see what it actually has.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: use of unitialized value
by Not_a_Number (Prior) on Oct 20, 2003 at 19:14 UTC

    By all means, use the above solutions. But as a general rule, it is easy to add print statements to your code when you have a problem:

    foreach my $itemcode (keys %inventory) { my $quantity = $inventory{$itemcode}[2]; print "<$quantity>\n"; # to replace if block }

    dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://300684]
Approved by chromatic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 09:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found