in reply to Re^2: creating defined references to arrays of size zero
in thread creating defined references to arrays of size zero

aha-- woke up and realized what I was doing-- definitely an ID 10-T error. Most of my keys were one letter, but one was actually a full word (I know the possible keys ahead of time), so I did something stupid like
hash = { a=>[]; b=>[] c=>[] . . . } #some time later my $key = "alpha" my $array = $hash->{$key};

At this point there isn't anything at $hash->{$key}, so $array isn't pointing to anything real. When I push onto $array, that memory will get allocated happily, and won't create anything in the hash (which is a very good thing). If instead, the first thing I do is to shove something into @{$hash->{$key}}, a new entry gets magically allocated, and then everything works as expected.

Obviously I'm not one with the perl yet.

Replies are listed 'Best First'.
Re^4: creating defined references to arrays of size zero
by NetWallah (Canon) on Jul 14, 2005 at 17:32 UTC
    This is precisely where tlm's suggestion(++) comes in handy:
    You could do:
    my $array = $hash->{$key} ||=[];
    This would initialize the hash value to an empty array ref, when necessary.

         "Income tax returns are the most imaginative fiction being written today." -- Herman Wouk