jfroebe has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have an array that holds multiple hashes... looking for a way to easily

example:

$sysmon_data[0]->{metadata_cache_mgmt}{max_open_databases}{count} $sysmon_data[0]->{cache_mgmt}{spinlock_contention}{per_sec}

What I would like to do is to make an anonymous array to easily access the lowest hash while avoiding a for/while loop if possible (I'm dealing with several hundred 'lowest hashes' - with possibly hundreds of array instances).

something like so (pseudo code):

$($sysmon_data->{cache_mgmt}{spinlock_contention})[0]->{per_sec}
basically I need an anonymous array of the "{per_sec}" in this instance.

ideas?

TIA

Edit: BazB fix code tags/reformat

Replies are listed 'Best First'.
Re: accessing an array with multiple hashes
by diotalevi (Canon) on Nov 11, 2003 at 20:49 UTC

    In the years since I've written this, UNIVERSAL::isa has been written. Use that instead.


    You might consider something along the lines of this explode() function. It finds the lowest level values and returns that as a list. This probably doesn't get to exactly what you want but its a starter idea for you anyway.

    use UNIVERSAL 'isa'; sub explode { map ref $_ ? explode( isa( $_, 'HASH' ) ? keys %$_ : isa( $_, 'ARRAY' ) ? @$_ : isa( $_, 'CODE' ) ? $_->() : isa( $_, 'SCALAR' ) ? $$_ : isa( $_, 'REF' ) ? $$_ : $_ ) : $_, @_; }
Re: accessing an array with multiple hashes
by ysth (Canon) on Nov 11, 2003 at 23:06 UTC
    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.
Re: accessing an array with multiple hashes
by ptkdb (Monk) on Nov 11, 2003 at 20:53 UTC
    @sysmon_data = ( { metadata_cache_mgmt => { max_open_databases => { co +unt => [ {}, {}, {} ] } }, cache_mgmt => { spinlock_contention => { per_sec => + [ {}, {}, {} ] } } } ) ;
    Doesn't that do it? The {}, {}, {} entry in per_sec would be addressable by index.
      The {}, {}, {} entry in per_sec is the hash levels down to the per_sec?
Re: accessing an array with multiple hashes
by Roger (Parson) on Nov 11, 2003 at 20:24 UTC
    Hi jfroebe, sounds like you just looking for a way to speed up your hash look up? You can have a look at building your structure with pseudo-hashes, which are faster to look up.

    Update: Thanks diotalevi, I wasn't aware that pseudo hashes are deprecated in 5.8, I am still using 5.6 on my Solaris box.

      Pseudo-hashes were deprecated in Perl 5.8.0 and they will be removed in Perl 5.10.0, see perl58delta for more details.