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



1arryb
User

Aug 24, 2010, 10:31 AM


Views: 5210
Re: [phone4u] FOR LOOP HELP

Hi phone,


Quote
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.

Instead of:

Code
if($line =~ m/address=>\'(.*?)\',\s*prefix=>\'(.*?)\',\s*data=>\'(.*?)\'/){  
...
}

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

Code
 
# Warning: untested, snippet.

my $lexo_hash = {};
foreach my $line (@lexo_content) {
chomp($line);
$line =~ /^address=>\'(.*?)\',\s*prefix=>\'(.*?)\',\s*data=>\'(.*?)\'/)/o;
next unless ( defined($1) and defined($2) and defined($3) );
my $n = $tr->add(address=>"$1", prefix=>"$2", data=>$3);
# Not sure if we're allowed to cache Net::Trie thingies, so convert to a hash.
$lexo_hash->{$n->{address}} = { address => $n->address, prefix => $n->prefix, data => $n->data };
}

foreach my $ip ( $ipv6_content ) {
chomp($ip);
my $a = $tr->add(address=>"$ip", prefix=>"128", data=>'blah'); # Assumes $a->data isn't significant.
next unless defined( $a->parent );
next unless exists($lexo_hash->{$a->parent->address});
print "\$a is ". $a->address . " and parent of \$a is "
. $a->parent->address . " and data of parents is: "
. $lexo_hash->{$a->parent->address}->{data} . "\n";
}


I hope this helps.

Cheers,

Larry


(This post was edited by 1arryb on Aug 24, 2010, 10:50 AM)


Edit Log:
Post edited by 1arryb (User) on Aug 24, 2010, 10:33 AM
Post edited by 1arryb (User) on Aug 24, 2010, 10:36 AM
Post edited by 1arryb (User) on Aug 24, 2010, 10:40 AM
Post edited by 1arryb (User) on Aug 24, 2010, 10:41 AM
Post edited by 1arryb (User) on Aug 24, 2010, 10:43 AM
Post edited by 1arryb (User) on Aug 24, 2010, 10:44 AM
Post edited by 1arryb (User) on Aug 24, 2010, 10:48 AM
Post edited by 1arryb (User) on Aug 24, 2010, 10: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