http://qs321.pair.com?node_id=553314


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:

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