I'm rewriting a large section of code and I would appreciate any impartment of wisdom from the Perl Monks on how to best design a data structure which can match the requirements necessary and be future-proofed enough that someone not familiar with the code base can add to it without tripping over their feet.

The data structure needs to be hierarchical and support both hashes and arrays. A quick example of what is needed is:

$galaxy{solarsystem}{planet}[year]{lat}{long}=’foo’;

Now, a simple hash of hashes of hashes… takes care of 95% of the requirements. The problem arises when I want to introduce an array as a parent of a hash. I know I can make a hash (of hashes of hashes) and put a reference to it in the array and be done with it. What I would like to do is eliminate some of that complexity so this data structure can be added to without that additional effort. i.e. the above assignment will fail due to pseudo-hashes being depreciated. (Note: they fail because year is an array. If year is a hash, everything is golden.)

An option that I could use is to simply shorten the depth of a majority of the tree such as:

$galaxy{solarsystem.planet}[year]{lat.long};

But I’m not sure if the added complexity in referencing inside foreach and similar loops is worth any speed improvement. I could combine this with converting the year to a hash and end up with a huge single level hash ($galaxy{solarsystem.planet.year.lat.long}) but that kind of defeats the purpose I’m trying to accomplish in the first place.

So.. I’m looking at what seem to amount to three options. First is to go with straight hashes and make sure array indexes are properly handled (which involves rewriting a non-trivial amount of code, but it is code that will probably need to be rewritten anyway). My second option is to implement the data structures ‘properly’ which may cause some confusion to non-programmers. My third option is to look around for or write a module that can load multi-dimensional hierarchal data from a configuration file (probably exists somewhere).

In order of importance, I want to code this to be:

  1. simple to understand and expand by non-programmers.
  2. hierarchal so segments of the tree can be referenced to and processed by subs.
  3. Syntaxically (is that a word?) valid through perl 5.10 (Pseudo-hashes were deprecated in Perl 5.8.0 and they will be removed in Perl 5.10.0, see perl58delta for more details.)

Anyone have any advice?


In reply to Hierarchial data structures by Q3Man

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.