Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: what does "use of uninitialized value" really mean?

by smls (Friar)
on Jul 13, 2013 at 12:54 UTC ( #1044151=note: print w/replies, xml ) Need Help??

in reply to what does "use of uninitialized value" really mean?

"Use of uninitialized value in array element" occurs when you use undef as an array index:

use strict; use warnings; my @array = (5, 10); my $index = undef; print $array[$index];

It would still output "5" (in addition to the warning), by interpreting undef as 0 - which may or may not be what you intended (hence the warning).

To get rid of the warning, make sure that any variable or expression that is used as an array index will never be undef. If the undef values occured by accident, fix the bug. If you're sure that undef values are no accident and that they should indeed be interpreted as 0, you can make that explicit for example using the // operator:

print $array[$index//0]

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1044151]
[LanX]: # Pestis Africana Suum
[erix]: that's an old plan, I think. Isn't there already a danewal (or some such name)?
[marto]: Build with -Wall
[erix]: Danewerk
[LanX]: I seem to remember that the danewall was build by the Frankish empire against the Danes
[erix]: 500 AD thru viking age
[erix]: that would make sense but wikipedia thinks otherwise
[LanX]: oh me was wrong ...
[erix]: but it always works both ways of course
[LanX]: Funny some weeks ago, I met people from the Cimbrian peninsula who didn't know it's called the Cimbrian peninsula ...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2018-03-22 18:42 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (283 votes). Check out past polls.