in reply to Re: Filling a hash
in thread Filling a hash

Actually, I use push to put items in a hash all the time. Of course, it's because I want a HoA so I can detect and deal with duplicates. One of the places I use this I then go through the HoA at the end, verify that all arrays have a single member, and then convert from HoA to straight H. I suppose I could do something like this:

@h{keys %h} = map { $_->[0] } values %h;
but I think I do this in a slightly longer piece of code.

In another location, I keep the arrays - I need to keep duplicate entries. Think of an XML file - each element can have multiple subelements. In my case, order between elements of different names doesn't matter, so I can put each name into a hash, and have an array of elements that all have that name as a child of the current element. (Does that even make sense?)

So, yes, I use push to populate hashes. It's not the usual way, but it's not uncommon.

(All actual examples of how I do it are removed so as not to give the OP any silly ideas to cut&paste...)

Replies are listed 'Best First'.
Re^3: Filling a hash
by polettix (Vicar) on Oct 26, 2005 at 14:26 UTC
    Uh, I was thinking about pushing both key and values, but yes, your solution is a viable one using push.
    All actual examples of how I do it are removed so as not to give the OP any silly ideas to cut&paste...
    Given the fact that the OP cannot use push, an example would be safe here :)

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.