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

I am playing around with the PCAP modules and was happily gathering data on machines on the network. But I was putting them into a HoH, keyed off the Ethernet MAC address. Like this:

$hash{MAC}{IP} $hash{MAC}{Port} $hash{MAC}{# of Packets} etc

It's not hard to see that this means that I can't see which IPs used which protocols, or how many packets were used for each protocol.

The obvious answer for the example above is: use a HoHoHoH

$hash{MAC}{IP}{Port}{#Packets}

but there are actually a few more variables involved, such as destination IP by source IP, destination port by source IP, etc . So I might be tempted by a data structure like this:

$hash{MAC}{IP}{Port}{Destination IP}{Port}{#Packets}

Obviously there are a few problems with this solution, including the fact that I might have to iterate over lots of sub-hashes, and it looks inelegant.

So what would you recommend? Should I maintain half a dozen separate hashes and update each one (possibly via a subroutine)? Should I create an object that will return data when called by methods like $obj->ports_by_ip($ip)?

____________________
Jeremy
I didn't believe in evil until I dated it.

Replies are listed 'Best First'.
(ichimunki) Re: What data structure should I use to describe networked computers?
by ichimunki (Priest) on Mar 03, 2001 at 19:35 UTC
    Even a badly normalized database will be easier to cope with mentally than keeping this in some one-size-fits-all variable construct. At least it would be for me. And SQL makes asking questions that include filtering, counting, summing, grouping, etc, etc, very easy. If it's not a lot of data, you could probably get by with DBI and DBD::CSV (the latter especially if you can create a single table).
      Or, if you go this route, DBD::RAM might suit your needs better. As the docs say:
      "DBD::RAM allows you to import almost any type of Perl data structure into an in-memory table and then use DBI and SQL to access and modify it."
      You'll find a helpful review of the module here.