Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by dimar (Curate)
on Jun 02, 2006 at 15:21 UTC ( [id://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.)

Rationale:

  • 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); };
=oQDlNWYsBHI5JXZ2VGIulGIlJXYgQkUPxEIlhGdgY2bgMXZ5VGIlhGV

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-23 12:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found