Hello Monks! I would like to know if someone has some idea how I could design the following in a nicer way:

I would like to build a data structure which is conceptually like a matrix, but where the rows and columns are identified by keys (like a hash). Actually, the columns are identified by various kinds of our platform, and the rows by properties of these platforms. For example, the element in column 'R-Solaris', row 'scriptpath', would be a path of directories containing scripts on platform of type R-Solaris, while the element in column 'R-Linux', row 'confdir', would be the path of some configuration directory used on systems of type R-Linux.

One obvious way to implement it would be to build a nested hash, like this:

my $conf = { scriptpath => { R-Solaris => '....', R-Linux => '....', UNC-Windows => '....', ... }, confdir => { R-Solaris => '....', R-Linux => '....', UNC-Windows => '....', ... }, ... }
This is certainly possible, but I don't like it very much, because it obsures the fact that we have a "matrix" (it's not obvious for the reader that every contained hash must have the same set of keys.

I was thinking of a variation of this solution in that I still use the data structure as outlined above, but don't construct it "statically", but with the help of constructor functions:

my $conf={}; sub conf { $conf{$_[0]}={ R-Solaris => $_[1], R-Linux => $_[2], UNC-Windows => $_[3], ... } } conf('scriptpath','...',...); conf('confdir',........);
This has the advantage that it not only becomes obvious, that the data is structured like a matrix, but that I can even check that the number of parameters is the correct one. Still, this looks to me like "too much code" for such a seamingly simple problem.

Of course those style questions are always are matter of personal taste, but if someone would solve this in a different way, I would like to see some alternative.

-- 
Ronald Fischer <ynnor@mm.st>

In reply to "Hash Matrix" Design Question by rovf

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.