Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Help with Hash of hashes, is there a better way?

by dimar (Curate)
on Jun 02, 2006 at 15:21 UTC ( #553314=note: print w/replies, xml ) Need Help??

in reply to Help with Hash of hashes, is there a better way?

As a general rule, I always use a flattened simple "Table" structure (AoH ref or alternatively an AoA ref) with denormalized data. Nested structures like the one you depicted are usually overkill unless there is some elaborate OOP going on behind the scenes. Sometimes I will use an (HoAoH ref) and call that a "Workbook", but rarely does the need to re-invent a data structure go beyond that unless it is intrinsic to code optimization or other ancillary design constraints.

(Note: subject to personal preference, ymmv, the following are instructive guidelines, but not laws.)


  • Everyone knows what a 'table' is, it's the "Hello World" of information architecture.
  • Just as every recursive routine can be expressed iteratively, so too can any nested data structure be mapped to zero or more related tables. Therefore, it is ontologically complete.
  • If you had "fun" devising 'just the right datastructure' that's a red flag that you over-engineered it.
  • When the inevitable change happens to your 'just right' data structure, adding or removing elements will be a major hassle, and will introduce breaking changes, unless the data structure is orthogonal to its semantic content.
  • You automatically get easy transfer between various reporting and querying apps, should the need arise (e.g., DBD::SQlite, DBD::AnyData, Template::Toolkit).
  • People can figure out what your code is doing just by looking at it.
  • The best datastructure is usually one that you don't have to think about much when you use it, just like you shouldn't have to use a special kind of glass to drink water instead of milk or juice.

Using this approach, any new 'data model' I have to work with usually consists of only four simple, reliable and repeatable steps: (define, populate, filter and process).

### define my $oTable000 = []; my $oRow = { 'typeof' => 'production', 'platform' => 'windows', 'foohost' => 'hostname', 'footarget' => 'targetname1', 'total_capacity' => 0, 'free_capacity' => 0, }; ### populate (with fake data) for (0 .. 12){ $oRow->{platform} = ${[qw(windows unix database)]}[int(rand(3))] +; $oRow->{typeof} = ${[qw(production development)]}[int(rand(2)) +]; my %hCurrent = %{$oRow}; $oTable000->[$_] = \%hCurrent; }; ### filter (do whatever querying or grouping you want here) @{$oTable000} = ### SORT BY sort { $a->{platform} cmp $b->{platform}} ### WHERE typeof = 'development' grep { $_->{typeof} eq 'development';} @{$oTable000}; ### process ### (send it off to your template engine, number crunch, whatever) foreach my $oRec (@{$oTable000}){ DoStuff($oRec); };

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://553314]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2023-10-05 00:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found