The only way in which my exact data structure is relevant is if nothing like it should ever be created -- i.e., if I am making it far more complicated than it needs to be. I haven't given enough details about my data for anyone to come to that conclusion. That wasn't wholly unintentional. I wanted the meditation to be about a general problem that I occasionally encounter, and was wondering if other people also encountered.

For the record, your proposed refactoring of the data structure wouldn't work in my case because 'Locations' is one key of many at the top level. The whole structure is intended to represent an empire, which controls a set of locations, has various technologies, a name, etc. Some of those are simple values, some are themselves nested structures. A 'Location' (identified by a name) has a set of buildings, but also resources, a description, an inventory, etc. So simplifying things in the manner you suggest would result in Locations colliding with the strings 'Name', 'Technologies', etc.

As for dereferences taking time -- um, my module uses a hash tie, and on every lookup it splits apart the key and does a recursive lookup. So it's far, far slower than just a set of dereferences! Performance is really not a concern. This is an exploration into readability.

But your comment about keys suggests that you misunderstand what I'm doing. The actual data structure is merely a HoHoHo...H. I am never storing any keys in their dotted form. Doing so would prevent me from grabbing out pieces of the structure:

$location_info = $h->{"Locations.$location"}; print "$location description: $location_info->{Description}\n";
I am just providing a hash reference that, in addition to being addressable the normal way, can also be addressed with an alternate dotted syntax. The internal structure is unchanged, and a deterministic traversal just requires a regular ASCIIbetical sort of the keys at each level (same as any other HoH.)

Sorry if this wasn't clear from the original post.


In reply to Re^4: Dotted hash access by sfink
in thread Dotted hash access by sfink

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.