in reply to accessing an array with multiple hashes
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.( ['metadata_cache_mgmt', 'max_open_databases', 'count', value], ['cache_mgmt', 'spinlock_contention', 'per_sec', value] )
Note that some error checking would be good. If you have a structure like:#!/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;
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.$sysmon_data[0]->{cache_mgmt} = 0; $sysmon_data[0]->{cache_mgmt}{spinlock_contention} = 'foo';
|
|---|