Option 1)

You could build an iterator, seperating the traversal from the client as demonstrated by the following:

{ my $iter = get_target_iter($stuff); while (my ( $env_name, $platform_name, $host_name, $target_name, $total_capacity, $free_capacity, ) = $iter->()) { local $, = "\t"; local $\ = "\n"; print $env_name, $platform_name, $host_name, $target_name, $total_capacity, $free_capacity; } }
sub get_target_iter { my ($envs, $env_name, $platforms, $platform_name, $hosts, $host_name, $targets, $target_name, $total_capacity, $free_capacity ); my (@envs, @platforms, @hosts, @targets); my $level; $envs = $_[0]; @envs = keys %$envs; $level = 0; return sub { for (;;) { if ($level == -1) { return; } if ($level == 0) { if (@envs) { $env_name = shift(@envs); $platforms = $envs->{$env_name}; @platforms = keys %$platforms; ++$level; } else { --$level; redo; } } if ($level == 1) { if (@platforms) { $platform_name = shift(@platforms); $hosts = $platforms->{$platform_name}; @hosts = keys %$hosts; ++$level; } else { --$level; redo; } } if ($level == 2) { if (@hosts) { $host_name = shift(@hosts); $targets = $hosts->{$host_name}; @targets = keys %$targets; ++$level; } else { --$level; redo; } } if ($level == 3) { if (@targets) { $target_name = shift(@targets); my $target = $targets->{$target_name}; $total_capacity = $target->{total_capacity}; $free_capacity = $target->{free_capacity}; } else { --$level; redo; } } return $env_name, $platform_name, $host_name, $target_name, $total_capacity, $free_capacity; } }; }

Tested.

Update: Below is an alternative iterator. It's a drop-in replacement for the above function. This version is much smaller thanks to Algorithm::Loops's NestedLoops.

use Algorithm::Loops qw( NestedLoops ); sub get_target_iter { my ($envs) = @_; my $iter = NestedLoops( [ [ keys %$envs ] , sub { [ keys %{$envs->{$_[0]} } ] }, sub { [ keys %{$envs->{$_[0]}{$_[1]} } ] }, sub { [ keys %{$envs->{$_[0]}{$_[1]}{$_[2]}} ] }, ], ); return sub { if (my ( $env_name, $platform_name, $host_name, $target_name, ) = $iter->()) { my $target = $envs->{$env_name } {$platform_name} {$host_name } {$target_name }; my $total_capacity = $target->{total_capacity}; my $free_capacity = $target->{free_capacity}; return $env_name, $platform_name, $host_name, $target_name, $total_capacity, $free_capacity; } return; }; }

Tested.


In reply to Re^3: Help with Hash of hashes, is there a better way? by ikegami
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.