On 11/22/2011 10:24 PM, Arvind wrote:
I have to write a perl script that keeps track of tcp connections. I
plan to keep track of this in an array of the form
tcp_connection[source address][dest address]
To do this I want to convert an IP address of the form A.B.C.D into a
number that will serve as an index of the array. I want to do it in a
way that there is very low probability of the number created from
A.B.C.D being the same as that created from E.F.G.H.
One way is to create a MD5 hash with input as A.B.C.D etc.
Is there any way to do this in perl?
regardless of the language, that is a poor way to track IP addresses. it
requires an array of 2^^32 in size to handle any possible IP address.
since you will likely not have so many actual entries you will be
wasting massive amounts of storage. this is known as a sparse array and
that is not what you want to use for this. the proper way (again, in
pretty much any language) is to use a hash or similar thing. a hash will
only grow in size based on the actual number of IP addresses and not the
maximum possible value. you just need to make sure you put the IP into
the same format for each one. then each entry will be unique and be a
good key for the hash. to learn more about this and the 2 level hash you
need, read perldoc perldata (for hashes), perldoc perlreftut (for
references) and perldoc perldsc for more on multilevel hashes.
uri
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/