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

Hi,

I have succesfully produced a data structure for a project at work, but I can't loop (traverse) through it so as to print it out!

The data structure is a hash of hashes of hashes. It looks like this:

%customers = (); $customers{$acct_id}{$ip}{'variableName'} = 'valueForVariable';
Basically, I have a few variables at the ip address level. $acct_id specifies accounts. Each account has a bunch of ip's. I have an array that holds every account ID. So, I want to do be able to loop through the hash for every ip at each given value for acct_id, so that I can print out the ip and various variable values, such as an snmp community read setting.

Does anyone know how I can traverse through such a data structure? I need to do so by iterating through the array that contains the account id's.

Here is an additional thing. I populated the data structure by passing a reference to it in a subroutine. I have no reason to do this here part in a subroutine, but I would be curious how to traverse through this data structure in two ways. One, by specifying the data structure directly. Two, by specifying it with a pointer to it.

As for me, I googled looping through hashes of hashes and perused this wonderful site, but didn't find anything that showed me how. I've made a number of attempts and remain pretty stuck.

Thanks in advance...

Tony (o2)

Replies are listed 'Best First'.
Re: traversing through a (complex for me) data structure
by madbombX (Hermit) on Aug 25, 2006 at 18:22 UTC
    The long way is to use a bunch of for loops:

    for my $acct_id (keys %customers) { print "Account: $acct_id\n"; for my $ip (keys %{$customers{$acct_id}}) { # Do stuff here with IP print "IP: $ip\n"; for my $id (keys %{$customers{$acct_id}{$ip}}) { print "ID: $id\n"; } } }

    As I frequently seem to be specifying, I like to visualize my data structure and how it's layed out. Just to ensure you are properly visualing your data structure, you may want to take a look at Re: How can I visualize my complex data structure?. Then take advantage of Data::Dumper to ensure that you are traversing your data structures in the proper manner.

    Eric

Re: traversing through a (complex for me) data structure
by derby (Abbot) on Aug 25, 2006 at 18:21 UTC

    Something like this (if your %customers is a HoHoH)

    #!/usr/bin/perl use strict; use warnings; my %customers = ( '1' => { '127.0.0.1' => { foo => 1 }, '127.0.0.2' => { foo => 2 }, '127.0.0.3' => { foo => 3 }, }, '2' => { '127.0.0.4' => { foo => 1 }, '127.0.0.5' => { foo => 2 }, '127.0.0.6' => { foo => 3 }, }, '3' => { '127.0.0.7' => { foo => 1 }, '127.0.0.8' => { foo => 2 }, '127.0.0.9' => { foo => 3 }, }, '4' => { '127.0.0.10' => { foo => 1 }, '127.0.0.20' => { foo => 2 }, '127.0.0.30' => { foo => 3 }, } ); my @ids = qw( 1 2 3 4 ); foreach my $id ( @ids ) { foreach my $ip ( keys %{ $customers{$id} } ) { print "ID: $id, IP: $ip, fo: $customers{$id}{$ip}{foo}\n"; } }

    -derby
Re: traversing through a (complex for me) data structure
by planetscape (Chancellor) on Aug 26, 2006 at 07:54 UTC
Re: traversing through a (complex for me) data structure
by McDarren (Abbot) on Aug 25, 2006 at 23:42 UTC
    If you are having difficulties grokking complex Perl data structures, there are two references that I would thoroughly recommend:
    1. The PDSC
    2. Intermediate Perl (the book)

    Cheers,
    Darren :)

Re: traversing through a (complex for me) data structure
by o2bwise (Scribe) on Aug 25, 2006 at 19:43 UTC
    Thanks, you guys.

    I am actually PRODUCTIVE again! :-)

    Much appreciated.

    Tony