CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced: Re: [phone4u] FOR LOOP HELP: Edit Log



deepeshtronics
Novice

Aug 3, 2010, 9:47 AM


Views: 5229
Re: [phone4u] FOR LOOP HELP

Hi,

I installed module "Net::IPTrie" in my machine and tested the code after doing some modifications. It seems that code is working fine.

Please use below code.


Code
#!/usr/bin/perl 

use warnings;
use strict;

use Net::IPTrie;
my $tr = Net::IPTrie->new(version=>6); # IPv6

open LEXO, "<", "lexo.txt" or die "Could not open file lexo.txt in read mode: $!\n";
open IPV6, "<", "IPv6.txt" or die "Could not open file IPv6.txt in read mode: $!\n";

my @lexo_content = <LEXO>;
my @ipv6_content = <IPV6>;

foreach my $ip (@ipv6_content) {
chomp $ip;
foreach my $line (@lexo_content) {
chomp $line;
if($line =~ m/address=>\'(.*?)\',\s*prefix=>\'(.*?)\',\s*data=>\'(.*?)\'/){
my $address = $1;
my $prefix = $2;
my $data = $3;
my $n = $tr->add(address=>"$address", prefix=>"$prefix", data=>"$data");
my $a = $tr->add(address=>"$ip", prefix=>'128', data=>"$data");;
if ($a->parent->address eq $n->address) {
print "\$a is ". $a->address . " and parent of \$a is ". $a->parent->address ." and data of parents is: ". $a->parent->data. "\n";
}
}
}
}

close LEXO;
close IPV6;


Input lexo.txt:

Quote
$tr->add(address=>'2001:1200::/32', prefix=>'64', data=>'16531');
$tr->add(address=>'2001:1210::/32', prefix=>'64', data=>'2549');
$tr->add(address=>'2001:1218::/32', prefix=>'64', data=>'278');

Input IPv6.txt:

Quote
2001:1200::1
2001:1218::3


OUTPUT

Quote
$a is 2001:1200::1 and parent of $a is 2001:1200::/32 and data of parents is: 16531
$a is 2001:1218::3 and parent of $a is 2001:1218::/32 and data of parents is: 278


I hope this serves your purpose.

Go through the code and test it properly. If now, some modification is needed on the basis of input patterns in the lexo.txt and IPv6.txt, I expect that from you.

Thanks


(This post was edited by deepeshtronics on Aug 3, 2010, 9:50 AM)


Edit Log:
Post edited by deepeshtronics (Novice) on Aug 3, 2010, 9:50 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives