You could use Objects instead of plain hashes. A few weeks ago i wrote an example for someone with a similar problem. It uses Moose for OO, and you can see how it simplifies the stringification of a nested structure:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; package SingleState; use Moose; has 'state' => (isa => 'Value', is => 'rw'); has 'value' => (isa => 'Value', is => 'rw'); has 'position' => (is => 'rw'); sub to_s { my $self = shift; return $self->state . "\t=>\t" . $self->value . "\n"; } package Stat; use Moose; has 'states' => (isa => 'ArrayRef', is => 'rw'); has 'title' => (is => 'rw'); has 'position' => (is => 'rw'); sub add_state { my $self = shift; return unless @_; push @{$self->{states}}, $_ for @_; } sub to_s { my $self = shift; my $o = $self->title ? "== " . $self->title . " ==\n" : ''; $o .= $_->to_s for (sort { $a->position <=> $b->position } @{$self +->states}); return $o; } package StatSet; use Moose; use Data::Dumper; has 'stats' => (isa => 'ArrayRef', is => 'rw'); sub add_stat { my $self = shift; return unless @_; push @{$self->{stats}}, $_ for @_; } sub report { my $self = shift; my $s; $s .= $_->to_s . "\n" for (sort { $a->position <=> $b->position } +@{$self->stats}); return $s; } package main; my $set = new StatSet; my $stat1 = new Stat ( title => 'CPU' ); my $stat2 = new Stat ( title => 'Memory' ); my $state1 = new SingleState( state => 'Idle', value => '92', position + => 1 ); my $state2 = new SingleState( state => 'User', value => '0', position + => 2 ); my $state3 = new SingleState( state => 'Cached', value => '531976', po +sition => 1 ); $stat1->add_state( $state1, $state2 ); $stat1->position(1); $stat2->add_state( $state3 ); $stat2->position(2); $set->add_stat($stat1); $set->add_stat($stat2); print $set->report;

In reply to Re: Help with Hash of hashes, is there a better way? by neniro
in thread Help with Hash of hashes, is there a better way? by TeraMarv

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.