It doesn't matter. Hash keys are stored internally only once, even if they are used across multiple hashes. So though it may seem wasteful to have two hashes with the same key in them, it's not. The same goes for multiple rows of anonymous hashes with the same keys. This is an internal, automatic, and invisible optimization, but a smart one at that.

Update: For the record and posterity, I wanted to mention that this optimization is known as "Shared strings", and is discussed in Advanced Perl Programming (the Cougar book), published by O'Reilly & Associates. Find it in Chapter 20: Perl Internals, Section 3.3.1. The exact quote is:

To prevent unnecessary duplication, the actual key strings are maintained in a systemwide shared string table (strtab in strtab.h). strtab is a simplified HV: each value here keeps a reference count of the number of uses of that string.

It's also discussed in http://www.faqs.org/docs/perl5int, where Simon Cozens writes the following in section 4.2.2.2:

The HEK stored inside a hash entry represents the key: it contains the hash value and the key itself. It's stored separately so that Perl can share identical keys between different hashes - this saves memory and also saves time calcu.llating the hash value.

Neither of those sources are as ubiquitous as a POD reference, but aside from unraveling the source, they seem to be the best insight we've got on the topic.


Dave


In reply to Re: How to tweak an AoH? by davido
in thread How to tweak an AoH? by neniro

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.