On Wed, Nov 26, 2008 at 23:50, Chas. Owens <[EMAIL PROTECTED]> wrote:
> On Mon, Nov 24, 2008 at 11:47, [EMAIL PROTECTED]
> <[EMAIL PROTECTED]> wrote:
>> Suppose I have C_IP address : 12.120.29.25
>>
>>
>> and I have list of following IP addresses :
>>
>> 212.120.128.0|19;
>> 12.120.0.0|15;
>> 12.120.16.0|20;
>> 12.120.72.0|22;
>> 12.120.96.0|20;
>> 12.120.40.0|21;
>> 12.120.0.0|21;
>> 12.120.192.0|19;
>> 12.120.16.0|22;
>> 12.120.36.0|22;
>> 12.120.80.0|20;
>> 194.212.120.0|21;
>> 212.120.32.0|19;
>> 212.120.64.0|18;
>> 212.120.192.0|19;
>> 213.3.12.120|29;
>> 116.212.120.0|24;
>> 12.120.24.0|21;
>>
>>
>> Now I need to map C_IP to list with longest prefix match. (As u can
>> there are many IP address with 12.120. but I need to map to one with
>> longest prefix match)
>
> The algorithm/data structure you are looking for is called a trie*.
> There are a few modules on CPAN that seem to implement tries for you:
> Data::Trie**, Tree::Trie***, Net::IPTrie****. Off hand, I would say
> Net::IPTrie looks like what you need.
>
> * http://en.wikipedia.org/wiki/Trie
> ** http://search.cpan.org/dist/data-trie/Trie.pm
> *** http://search.cpan.org/dist/Tree-Trie/Trie.pm
> **** http://search.cpan.org/dist/Net-IPTrie/lib/Net/IPTrie.pm
>
>
> --
> Chas. Owens
> wonkden.net
> The most important skill a programmer can have is the ability to read.
>
It looks like it is fairly easy as well, but I haven't checked to see
if 12.120.24.0|21 is the right answer:
#!/usr/bin/perl
use strict;
use warnings;
use Net::IPTrie;
my $tr = Net::IPTrie->new(version => 4);
while (<DATA>) {
chomp;
my ($ip, $prefix) = split /\|/;
$tr->add(address => $ip, prefix => $prefix, data => $_);
}
print $tr->find(address => shift)->data, "\n";
__DATA__
212.120.128.0|19
12.120.0.0|15
12.120.16.0|20
12.120.72.0|22
12.120.96.0|20
12.120.40.0|21
12.120.0.0|21
12.120.192.0|19
12.120.16.0|22
12.120.36.0|22
12.120.80.0|20
194.212.120.0|21
212.120.32.0|19
212.120.64.0|18
212.120.192.0|19
213.3.12.120|29
116.212.120.0|24
12.120.24.0|21
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/