rovf has asked for the wisdom of the Perl Monks concerning the following question:
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:
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.my $conf = { scriptpath => { R-Solaris => '....', R-Linux => '....', UNC-Windows => '....', ... }, confdir => { R-Solaris => '....', R-Linux => '....', UNC-Windows => '....', ... }, ... }
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:
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.my $conf={}; sub conf { $conf{$_[0]}={ R-Solaris => $_[1], R-Linux => $_[2], UNC-Windows => $_[3], ... } } conf('scriptpath','...',...); conf('confdir',........);
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: "Hash Matrix" Design Question
by jethro (Monsignor) on Sep 24, 2008 at 13:22 UTC | |
by rovf (Priest) on Sep 24, 2008 at 13:28 UTC | |
by jethro (Monsignor) on Sep 24, 2008 at 14:00 UTC | |
|
Re: "Hash Matrix" Design Question
by pajout (Curate) on Sep 24, 2008 at 16:02 UTC |