G'day Monks,

I have been writing a script to monitor the capacities of various disks and databases in my work environment. During this process i have found myself working with the following Hash of Hashes

$stuff = { production => { windows => { Hostname => { targetname1 = { total_capacity = value, free_capacity = value }, targetname2 = { total_capacity = value, free_capacity = value } } }, unix => { Hostname => { targetname1 = { total_capacity = value, free_capacity = value }, targetname2 = { total_capacity = value, free_capacity = value } } }, database => { Hostname => { targetname1 = { total_capacity = value, free_capacity = value }, targetname2 = { total_capacity = value, free_capacity = value } } } }, development => { windows => { etc. etc.}, unix => { etc. etc.}, database => { etc. etc.} } } ;

While this perfectly describes the information i'm working with it becomes a bit unwieldy when working with it. I have had to resort to a bunch of nested 'while' loops

while(my($env,$platform_href) = each %{$stuff}) { while(my($platform,$host_href) = each %{$platform_href}) { while(my($host,$target_href) = each %{$host_href}) { while(my($target,$capacity_href) = each %{$target_href}) { # Do stuff with # $env # $platform # $host # $target # $capacity_href->{'total_capacity'}; # $capacity_href->{'free_capacity'}; } } } }

Nasty eh?

Is there a better/prettier way of traversing the data structure or even a better way of representing the data with a different structure?

Many Thanks,

TeraMarv


In reply to 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.