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

Hi all,
I am looking to create an unknown number of hashes that represent MAC addresses to IP address and a hostname. For example:
0810100292{192.168.1.1}=mybox.myhome.com

Assuming I can do this, how do I print this unknown number of hashes. The key to this is that these is the hash names are the only ones numerically represented. So finding them ought to be easy. Most everyone can print a standard hash table, by doing this..
foreach my $key (keys %conf){ print $key, "= $conf{$key}"; }

But how do I do this for multiple "unknown" hashes? Is this the smart way or should I use a standard array? By the way if you want to throw in the code to populate the array, I'm "ok" with that too:)

Rhodium

The seeker of perl wisdom.

Replies are listed 'Best First'.
Re: Printing multiple unknown hashes..
by DamnDirtyApe (Curate) on Jul 03, 2002 at 21:06 UTC

    I think you might be well served by using a HoH (hash-of-hashes) for this. Consider the following data structure:

    $VAR1 = { '0810100293' => { 'ip' => '192.168.1.2', 'host' => 'otherbox.myhome.com' }, '0810100294' => { 'ip' => '192.168.1.3', 'host' => 'thisbox.myhome.com' }, '0810100295' => { 'ip' => '192.168.1.4', 'host' => 'thatbox.myhome.com' }, '0810100292' => { 'ip' => '192.168.1.1', 'host' => 'mybox.myhome.com' } };

    This should allow you to do simple lookups of the IP addresses and host names, and all contained in one variable.


    _______________
    D a m n D i r t y A p e
    Home Node | Email
Re: Printing multiple unknown hashes..
by mephit (Scribe) on Jul 03, 2002 at 21:15 UTC
    Hmm, what you're suggesting smells of symbolic references, which should be avoided. Why not use a hash of hashes? The "top-level" keys would be the MAC address, and the value would be an anonymous hash with the IP for the key and the hostname for the value, like such:
    %hash = ( 0810100292 => { "192.168.1.1" => "mybox.myhome.com" }, 0810100293 => { "192.168.1.1" => "foo.myhome.com" }, }
    There's your "unknown number of hashes" all in one data structure. HTH.

    Update Bah! I take too damn long to test the code that I post...

    --

    There are 10 kinds of people -- those that understand binary, and those that don't.

      ++ for actually testing your posted code!

      "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
Re: Printing multiple unknown hashes..
by caedes (Pilgrim) on Jul 03, 2002 at 21:07 UTC
    There really isn't any reason to make separate hashes with the MAC addresses as the names. Since you seem to be only associating one MAC address with one IP address and one hostname, a better solution would be to use a hash of arrays. The hash keys could be the IP address (or whatever you want to look up by), and the array could be (MACaddress, hostname). You would then print the info like so:
    for $key (%hash){ print "$key $hash{$key}->[0] $hash{$key}->[1]\n"; }

    -caedes

Re: Printing multiple unknown hashes..
by ehdonhon (Curate) on Jul 03, 2002 at 21:10 UTC

    I'd use nested hashes. The first layer is a hash with all of your MAC addresses as the keys, and the values are your second hash which map the keys 'ip' and 'host' to the ip address and hostname, respectively.

    foreach my $mac ( keys %CODES ) { printf " %s: Host = %s, IP = %s\n", $mac, $CODES{$mac}->{'host'}, $CODES{$mac}->{'ip'}; }