The part with $tr->add(address=>'2a02:.........); is saved in a file lexo.txt becos they are a lot of addresses to be added, also part with with my $a =$tr->add(address=>'2a02:.......); saved in another file which contains a lot addresses to be added too. So I need help with for loop that will run through each saved .txt files. Thanks

Thanks for your urgent response, but i didnt seem to get it working. For the first text, it was saved as lexo.txt and I want it to replace; $tr->add(address=>'2a02:ee0::/32', prefix=>64, data=>'43333'); $tr->add(address=>'2a02:e98::/32', prefix=>64, data=>'24641'); as I have thousands of them saved in file lexo.txt already. And for this line; my $a = $tr->add(address=>'2a02:ee0::2', data=>$data); I have all the IPv6 (2a02:ee0::2)saved in a text file called IPv6.txt, I want the script to read each v6 addresses from IPv6.txt to perform the function following it. I hope you have an idea of what I intend doing now, I am a beginner in perl and i will appreciate if you can help me out by putting the file-names to the script. So that it will look like;

and here's content of IPv6.txt; 2001:1250::12 2001:12f0:300:1::151 2001:12f0:300:1::132 2001:12ff:0:2::3 2001:12ff:0:4::22 2001:12ff::1 2001:12ff:0:2::3 2001:12ff:0:4::22 2001:13c7:7001:4000::3 2001:13c7:7002:4000::10 2001:1400::1 2001:1418:100:1b5::2 2001:1418:100:1cf::2 2001:1418:100:22::2 2001:1418:100:359::2 2001:1418:100:d4::2 2001:1418:10:5::2 The Perl script aim is to do a look-up from lexo.txt trie, so for each line in IPv6.text the script will check which prefix and AS number they belong to according to trie added from lexo.txt. If everything is ok, the script should print corresponding prefix and AS from lexo.txt for each IP addresses in IPv6.txt

I have never used the module "Net::IPTrie" but i suppose that following code should serve you purpose.

My understanding for your problem goes as follows.

That lexo.txt contains the base address of ips (from IPv6.txt). I mean it contains the class. Now you first want to identify the class (from lexo.txt) for an ip (from IPv6.txt) then you want to fetch the prefix and AS from the class.

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";

hi thanks a lot for your, it doesn't seem to work but I decided to paste the file lexo this way and try to use the for-loop to read the IPV6.txt , but each time the IP isn't available on the lexo list, it stops and give this error;

"Can't call method "address" on an undefined value at b.pl line 3204, <IPV6> line 4428."

Now, I hope you can help me with additional script that will ignore this error and continue with other IPs in the list till it reach the end.

You are indeed a saviour, thanks a lot for your help.......it works perfectly now but only for some certain IPs like the one you listed, while some other IPs on the list aint working but I guess i need to update the prefix list(lexo.txt), in the mean time, I wish the script can ignore this error(Can't call method "address" on an undefined value at guru.pl line 23, <IPV6> line 4428.) and continue with other IPs that will work till the end of the list.

Hi, Thanks for all your help, please I need one last help pleaseeeeee. Please, I want the script to save file lexo.txt in the memory of this script and the file IPv6.txt will read only from that memory. At the present, I discovered that the content of file IPv6.txt is reading through lexo.txt from the begining each time which takes longer time like days. Its suppose to take all the files in IPv6.txt once to read through lexo.txt and all the output appears in few seconds. here is what have done so far; #!/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";

I want the script to save file lexo.txt in the memory of this script and the file IPv6.txt will read only from that memory.

I do not understand this comment. You already read lexo.txt to an array here:

Code

my @lexo_content = <LEXO>;

so that is not the problem.

What is a problem is that the Big O of this program is 2, and the regular expression is inside the inner loop. That means you are doing <number of lines in ipv6.tx> x <number of lines in lexo.txt> regular expressions. You haven't noted them, but this program makesTWICE as many calls to $tr->add() as it does regular expressions! I'm sure that's significant.

The first thing to try is to break up the regular expressions to reduce their complexity and obviate the unnecessary ones.

You could try testing for a likely line to parse using a much simpler (and faster) expression:

Code

# Notes: '^' anchors the expression at line start, simplifying the line scan. # 'o' precompiles the regex so it executes faster on the 2nd and succeeding iteration. if ( $line =~ /^address=>/o ) { $line =~ /^address=>\'(.*?)\',\s*prefix=>\'(.*?)\',\s*data=>\'(.*?)\'/)/o; ... }

By refactoring, we can drastically reduce the number of both regular expressions and $tr->add calls.

If you pre-parse lexo.txt into a hash keyed by ip you reduce the number of regular expressions to <number of lexo.txt lines> and reduce the number of $tr->add() calls to <number of lexo.txt lines> + <number of ipv6.tx lines>: