Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: wondering what exactly these lines does

by mowgli (Friar)
on Feb 26, 2003 at 17:05 UTC ( [id://238844]=note: print w/replies, xml ) Need Help??


in reply to nested hashes and their usage e.g $foo{bar}{baz}

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! :)

--
mowgli

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://238844]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-03-28 22:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found