http://qs1969.pair.com?node_id=474118

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

Update:Made a minor correction to the code example. (Thanks Frodo72)
I've been finding myself building a lot of hashes of hashes of hashes ... of arbitrary depth lately, and i dont think my scheme is the greatest. So im wondering if somebody has a better scheme or if im on the right track.

Here's a simplified version of what i am basically doing:

use strict; use warnings; my %big_hoh; while ( my $data = get_one_record(...) ) { my @key_list = get_arbitrary_length_key_list($data, ...); my $last_key = pop @key_list; my $storage_point = \%big_hoh; foreach my $key (@key_list) { if ( ! exists $storage_point->{$key} ) { $storage_point->{$key} = {}; $storage_point = $storage_point->{$key}; } } $storage_point->{$last_key} = $data; }
This works but it seems pretty nasty to me. If $data were not a hashref, im not sure that it would work properly. I thought about building up from the back end, but that seems like it will be even worse.

This is for building a data cache for a system where users can do various searches in a MySQL DB, and sometimes it makes sense to cache some of the data, to take some load off the DB server. This is the 3rd major revision of the engine, and this revision is mostly optimizations.

Replies are listed 'Best First'.
Re: Hash of Hash of Hash of ...
by merlyn (Sage) on Jul 11, 2005 at 22:56 UTC
      Thanks for the link, that thread has a lot of good info in it. I will probably use the recursive tree implementation. I do need it to be tree-like, and not joined keys because there will be access at various levels depending on the situation.

      Sorry for not doing enough RTFM, i was struggling with what keywords to search on, and multidimensional hash never occured to me.

Re: Hash of Hash of Hash of ...
by tlm (Prior) on Jul 12, 2005 at 00:06 UTC
      There are also several pre-written implementations of this on CPAN. My Data::DRef module has functions to get and set values based on multiple-key lists like you're working with:
      use Data::DRef qw( set_value_for_keys ); set_value_for_keys( \%big_hoh, $data, @key_list );
Re: Hash of Hash of Hash of ...
by polettix (Vicar) on Jul 12, 2005 at 00:33 UTC
    Apart from the solution pointed to by tlm, note that you have an error in your code:
    use strict; use warnings; my %big_hoh; while ( my $data = get_one_record(...) ) { my @key_list = get_arbitrary_length_key_list($data, ...); my $last_key = pop @key_list; my $storage_point = \%big_hoh; foreach my $key (@key_list) { if ( ! exists $storage_point->{$key} ) { $storage_point->{$key} = {}; $storage_point = $storage_point->{$key}; } } # Added to kick next line $storage_point->{$last_key} = $data; # out of the cycle }

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
      Thanks, i wrote this simplified version based on a piece of my actual code. no one wants to see that, when it's done i hope to never have to touch it again. The real usage is encapsulating some really nasty stuff. :)
Re: Hash of Hash of Hash of ...
by anonymized user 468275 (Curate) on Jul 12, 2005 at 07:25 UTC
Please help Can't locate GTop.pm in @INC
by aladdin_millenium (Initiate) on Jul 12, 2005 at 06:31 UTC
    Hi,

    I've to write a PERL Script that should invoke a GUI and then See the "Peak Memory Usage" / "CPU Time" consumed by the exe and print the same.

    I'm trying to use the module GTOP, but when i run the script

    ####################### use GTop (); my \$gtop = GTop->new; #######################

    the following errors are displayed

    Can't locate GTop.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site/ +lib .) at C:\Program Files\Interra Systems\Interra H264 Analyzer\test +1.pl line 1. BEGIN failed--compilation aborted at C:\Program Files\Interra Systems\ +Interra H264 Analyzer\test1.pl line 1.

    I also tried to use the command

    PPM> Install GTOP

    but it says "Searching for 'GTOP.pm' returned no results. Try a broader search first."

    Please help and treat this as urgent.

    Edit g0n - added code & p tags

      Likely, GTop is not installed on your machine, which is little surprising, because libgtop is a library out of the Gnome project which only exists for Unix, or maybe even only for Linux.

      Depending on what you want to do, maybe Win32::Process or Win32::Process::Info might be of help for you.

      As an aside, your post was in reply to a completely unrelated post. Please read the Perlmonks FAQ and maybe some introductory node, like turnsteps home node.