in reply to Exists and arrays

Thanks everyone for your replies

I was assuming that the initialization of elements 0 to 5 was implicitly done by perl.

From Learning Perl 5th Ed. If you store an array element that is beyond the end of the array, the array is automatically extended as needed - there's no limit on its length, as long as there's available memory for Perl to use. If Perl needs to create intervening elements, it creates them as undef values.

So this led me to expect that exists would work here while defined won't. Of course the dumper output doesn't help the confusion.

Replies are listed 'Best First'.
Re^2: Exists and arrays
by JadeNB (Chaplain) on Oct 03, 2009 at 21:18 UTC
    If you store an array element that is beyond the end of the array, the array is automatically extended as needed - there's no limit on its length, as long as there's available memory for Perl to use. If Perl needs to create intervening elements, it creates them as undef values.
    This is just correct enough to be misleading. Assigning to an OOB element of an array @a modifies $#a, and (I assume) Data::Dumper is using $#a to decide how big @a is. (See STORESIZE in perltie.) It's true that trying to access ‘intervening’ elements will return undef, but it's not true that those elements were ‘created’ by the STORESIZE.