I hope what follows is what you are looking for. I searched CPAN for utility modules to deal with nested hash structures, and came up empty--perhaps my search skills are just not up to it. Anyway, I have two utility functions. One flattens out a tree--for instance, your $sysmon_data[0] comes out like this:
( ['metadata_cache_mgmt', 'max_open_databases', 'count', value], ['cache_mgmt', 'spinlock_contention', 'per_sec', value] )
where the values are what you get from looking up that series of keys. The other reapplies the keys to a (potentially initially undefined) hashref and returns a reference to the node found/created.
#!/usr/bin/perl use warnings; use strict; # takes ref to zero-or-more levels deep nested hash structure # returns list of array refs for each leaf containing, top down, the k +eys # to reach the node followed by the leaf value sub leaves { my $node = shift; if (ref $node eq 'HASH') { my @leaves; for my $key (keys %$node) { push @leaves, map [$key, @$_], leaves($node->{$key}); } @leaves; } else { [$node]; } } # takes ref to nested hash structure and list of top-down keys to trav +erse # returns reference to to desired node sub find_node_ref { my $noderef = \$_[0]; shift; $noderef = \($$noderef->{$_}) for @_; $noderef; } our @in; our $out; $in[0]{foo}{bar}{baz} = 'bag'; $in[0]{foo}{fuw} = 'fu'; $in[1]{foo}{gar} = 'weeble'; $in[2]{foo}{bar}{baz} = 'bog'; for (my $idx = 0; $idx < @in; ++$idx) { next if not defined $in[$idx]; for my $leaf (leaves($in[$idx])) { my $value = pop @$leaf; my $node = find_node_ref($out, @$leaf); $$node = [] unless defined $$node; $$node->[$idx] = $value; } } use Data::Dumper; print Dumper \@in; print Dumper $out;
Note that some error checking would be good. If you have a structure like:
$sysmon_data[0]->{cache_mgmt} = 0; $sysmon_data[0]->{cache_mgmt}{spinlock_contention} = 'foo';
you will end up trying to use a node in $out as an array ref after setting it to a numeric value, resulting in an error. A given sequence of keys should either always or never be a leaf node.

In reply to Re: accessing an array with multiple hashes by ysth
in thread accessing an array with multiple hashes by jfroebe

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.