in reply to push onto hash!

use perlfunc:ref to see if the value of $somehash{"$foo$bar"} is an arrayref. If it isn't, then create it.

unless ( ref ($somehash{"$foo$bar"} eq 'ARRAY' ) { $somehash{"$foo$bar"} = []; } ...

HTH

Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Replies are listed 'Best First'.
(Ovid - don't use 'ref') Re(2): push onto hash!
by Ovid (Cardinal) on Apr 03, 2001 at 20:32 UTC

    As noted later, your use of ref is not necessary because you can autovivify the hash entry and push onto it in one step:

    push @{$somehash{"$foo$bar"}}, $x;

    However, another point is to reconsider ever using ref. As was made clear to me this node, most uses of ref indicate that the code design should be revisited. ref is typically used to handle "special cases" and make things magically "work", which typically suggests a design flaw. Go to that link and read it. Good stuff! Once I grokked it, I rewrote the main loop in the code that I was making my point with and discovered that my new loop, without using ref, was smaller and easier to maintain.

    Cheers,
    Ovid

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