Keep It Simple, Stupid | |
PerlMonks |
Re: wondering what exactly these lines doesby mowgli (Friar) |
on Feb 26, 2003 at 17:05 UTC ( [id://238844]=note: print w/replies, xml ) | Need Help?? |
You've got a hash of hashes (sometimes called HoH) here - basically, %srciphash is a hash (an associative array) that contains references to further hashes. The first line you merely records whether a connection was made from a given source to a given destination ip (assuming that's what "srcip" and "dstip" stand for, which I think is pretty likely), by setting the appropriate element of the appropriate hash (itself an element in %srciphash) to 1. The second line later on iterates over all the source ips gathered; for that, the hash keys (the source ips) are extracted from the hash via the keys operator, and fed to a foreach loop which iterates over them one by one, assigning each of them to $srcip for the duration of the loop. The third line is quite similar to the second; an iteration over the keys of a hash (the destination ips for a given source ip this time) is made again, and the only real difference is that the hash these come from needs to be obtained first. In other words - since $srciphash{$srcip} (the hash value associated with the given source ip in %srciphash) is itself a reference to a hash (and thus a scalar value) instead of an immediate hash, it needs to be made into a hash again first for the keys operator to be able to work on it; that's simply done by prepending a % (and wrapping the whole thing in curly braces to avoid ambiguities). Then, a foreach loop is used again to iterate over all the destination ips for the given source ip (the keys of the hash that is the value associated with the source ip in %srciphash), so, in effect, the two nested loops just iterate over all pairs of src and destination ips that were encountered found earlier on. The actual value stored (the 1) is not used - it could've been any value really, too, as it just serves the purpose of making sure the given *key* actually exists in the hash. Hope this helps! :)
--
In Section
Seekers of Perl Wisdom
|
|