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...)


In reply to Re^2: Filling a hash by Tanktalus
in thread Filling a hash by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.