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: Intermediate:
FOR LOOP HELP

 



phone4u
Novice

Jul 31, 2010, 1:28 PM

Post #1 of 13 (1322 views)
FOR LOOP HELP Can't Post

Hi, I need urgent help in writing a for-loop for a perl script below;
Code

#!/usr/bin/perl
use Net::IPTrie;
my $tr = Net::IPTrie->new(version=>6); # IPv6

$tr->add(address=>'2a02:ee0::/32', prefix=>64, data=>'43333');
$tr->add(address=>'2a02:e98::/32', prefix=>64, data=>'24641');

my $a = $tr->add(address=>'2a02:ee0::2', data=>$data); # prefix defaults to 32
print "\$a is ". $a->address . " and parent of \$a is ". $a->parent->address ." and data of parents is: ". $a->parent->data. "\n";


die;


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


AnkurMundhada
Novice

Aug 5, 2010, 4:25 AM

Post #2 of 13 (1271 views)
Re: [phone4u] FOR LOOP HELP [In reply to] Can't Post

hi ,

Try while loop which will iterate through the IP range ...

Code
my $ip = new Net::IP($newIPRange) ; 
do{
print $ip->ip();
}while(++$ip);

Thanks
Ankur
Thanks
Ankur M.


phone4u
Novice

Aug 6, 2010, 4:28 AM

Post #3 of 13 (1204 views)
Re: [AnkurMundhada] FOR LOOP HELP [In reply to] Can't Post

Hi, Thanks for your help, please I need one more 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";

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

foreach my $ip (@ipv6_content) {
chomp $ip;
foreach my $line (@lexo_content) { ........think the problem is here
chomp $line; ..............think the problem is here
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 (defined $a->parent && $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;


Thanks


AnkurMundhada
Novice

Aug 6, 2010, 4:59 AM

Post #4 of 13 (1201 views)
Re: [phone4u] FOR LOOP HELP [In reply to] Can't Post

HI,

As per my understanding you can write the code as.
1) reading the lexo.txt and placing it into hash.
2) then reading the other file.
3) then comparing the result.


Code
#!/usr/bin/perl  

use warnings;
use strict;

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

my %lexoAddHash = ();

open LEXO, "<", "lexo.txt" or die "Could not open file lexo.txt in read mode: $!\n";
my @lexo_content = <LEXO>;
foreach my $line (@lexo_content)
{
chomp $line;
if($line =~ m/address=>\'(.*?)\',\s*prefix=>\'(.*?)\',\s*data=>\'(.*?)\'/){
my $n = $tr->add(address=>"$1", prefix=>"$2", data=>"$3");
$lexoAddHash{$n->address} = $n->address;
}
}
close LEXO;

open IPV6, "<", "IPv6.txt" or die "Could not open file IPv6.txt in read mode: $!\n";
my @ipv6_content = <IPV6>;
foreach my $ip (@ipv6_content) {
chomp $ip;
foreach my $line (@lexo_content) { # ........think the problem is here
my $a = $tr->add(address=>"$ip", prefix=>'128', data=>"$data");;
if (defined $a->parent && defined $lexoAddHash{$a->parent->address}) {
print "\$a is ". $a->address . " and parent of \$a is ". $a->parent->address ." and data of parents is: ". $a->parent->data. "\n";
}
}
}
}
close IPV6;


Please let me know if you need any help. Smile
Thanks
Ankur M.


phone4u
Novice

Aug 6, 2010, 5:47 AM

Post #5 of 13 (1198 views)
Re: [AnkurMundhada] FOR LOOP HELP [In reply to] Can't Post

Hi, Thanks for urgent response. I added a new line becos it was giving error;
But still, it reads only first line of the file IPv6.txt and printed it repeatedly like printing output for all the lines in IPv6.txt;

#!/usr/bin/perl

use warnings;
use strict;

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

my %lexoAddHash = ();

open LEXO, "<", "lexo.txt" or die "Could not open file lexo.txt in read mode: $!\n";
my @lexo_content = <LEXO>;
foreach my $line (@lexo_content)
{
chomp $line;
if($line =~ m/address=>\'(.*?)\',\s*prefix=>\'(.*?)\',\s*data=>\'(.*?)\'/){
my $n = $tr->add(address=>"$1", prefix=>"$2", data=>"$3");
$lexoAddHash{$n->address} = $n->address;
}
}
close LEXO;

open IPV6, "<", "IPv6.txt" or die "Could not open file IPv6.txt in read mode: $!\n";
my @ipv6_content = <IPV6>;
foreach my $ip (@ipv6_content) {
chomp $ip;
foreach my $line (@lexo_content) { # ........think the problem is here
my $data = $3; #....i just added this line becos it was requesting for $data in line 28
my $a = $tr->add(address=>"$ip", prefix=>'128', data=>"$3");;
if (defined $a->parent && defined $lexoAddHash{$a->parent->address}) {
print "\$a is ". $a->address . " and parent of \$a is ". $a->parent->address ." and data of parents is: ". $a->parent->data. "\n";
}
}
}

close IPV6;


Please,I want the file lexo.txt saved in the memory of the script or as part of this script as if all its inputs are printed under this line;
use Net::IPTrie;
my $tr = Net::IPTrie->new(version=>6); # IPv6
# ......saved around here

then, each line or IPs in IPv6.txt goes through the saved lexo.txt file for a match of "prefix" and "data" as included in the script.

Thank you very much for your anticipated help.


phone4u
Novice

Aug 6, 2010, 6:07 AM

Post #6 of 13 (1194 views)
Re: [phone4u] FOR LOOP HELP [In reply to] Can't Post

I tried debugging the script by trying print $ip and I discovered its print only the first line in the saved IPv6.txt repeatedly while print $a and print $n were giving errors.
here are some content of my files so you can probably try it out on your machine;
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
2001:1418:10:6::131
2001:1418:1:100::6
2001:1418:1:101::1

lexo.txt;
$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');
$tr->add(address=>'2001:1228::/32', prefix=>'64', data=>'18592');
$tr->add(address=>'2001:1250::/32', prefix=>'64', data=>'28497');
$tr->add(address=>'2001:1258::/32', prefix=>'64', data=>'28499');
$tr->add(address=>'2001:1284::/32', prefix=>'64', data=>'14868');
$tr->add(address=>'2001:1291::/32', prefix=>'64', data=>'16735');
$tr->add(address=>'2001:1298::/32', prefix=>'64', data=>'28296');
$tr->add(address=>'2001:129c::/32', prefix=>'64', data=>'22356');
$tr->add(address=>'2001:12a4::/32', prefix=>'64', data=>'28346');
$tr->add(address=>'2001:12b0::/32', prefix=>'64', data=>'28618');
$tr->add(address=>'2001:12c0::/32', prefix=>'64', data=>'11706');
$tr->add(address=>'2001:12c4::/32', prefix=>'64', data=>'28262');
$tr->add(address=>'2001:12c8::/32', prefix=>'64', data=>'21911');
$tr->add(address=>'2001:12d0::/32', prefix=>'64', data=>'28571');
$tr->add(address=>'2001:12d8::/32', prefix=>'64', data=>'1251');
$tr->add(address=>'2001:12e8::/32', prefix=>'64', data=>'16397');
$tr->add(address=>'2001:12f0:300::/48', prefix=>'64', data=>'2716');


(This post was edited by phone4u on Aug 6, 2010, 6:27 AM)


AnkurMundhada
Novice

Aug 6, 2010, 7:08 AM

Post #7 of 13 (1185 views)
Re: [phone4u] FOR LOOP HELP [In reply to] Can't Post

Hi,

Since $3 cannot be use as you have used here it will always have the last row data in it.

you are creating $a with the help of $data which we are extracting if from the file "lexo.txt". i think you need to add one more for loop to achieve this.


Code
  

#!/usr/bin/perl

use warnings;
use strict;

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

my %lexoAddHash = ();
my @lexoDataHash = ();
open LEXO, "<", "lexo.txt" or die "Could not open file lexo.txt in read mode: $!\n";
my @lexo_content = <LEXO>;
foreach my $line (@lexo_content)
{
chomp $line;
if($line =~ m/address=>\'(.*?)\',\s*prefix=>\'(.*?)\',\s*data=>\'(.*?)\'/){
my $n = $tr->add(address=>"$1", prefix=>"$2", data=>"$3");
$lexoAddHash{$n->address} = $n->address;
push(@lexoDataHash,$3);
}
}
close LEXO;



open IPV6, "<", "IPv6.txt" or die "Could not open file IPv6.txt in read mode: $!\n";
my @ipv6_content = <IPV6>;
foreach my $ip (@ipv6_content) {
chomp $ip;
foreach my $line (@lexo_content) { # ........think the problem is here
foreach my $data (@lexoDataHash){
my $a = $tr->add(address=>"$ip", prefix=>'128', data=>"$data");
if (defined $a->parent && defined $lexoAddHash{$a->parent->address}) {
print "\$a is ". $a->address . " and parent of \$a is ". $a->parent->address ." and data of parents is: ". $a->parent->data. "\n";
}
}

}
}

close IPV6;



I think that will be the final solution for the above problem.... Smile
Thanks
Ankur M.


phone4u
Novice

Aug 6, 2010, 7:24 AM

Post #8 of 13 (1178 views)
Re: [AnkurMundhada] FOR LOOP HELP [In reply to] Can't Post

Its all good now apart from it outputting/printing only the first line of the IPv6 address like this and ignoring others in the list;
$a is 2001:1250::12 and parent of $a is 2001:1250::/32 and data of parents is: 28497
$a is 2001:1250::12 and parent of $a is 2001:1250::/32 and data of parents is: 28497
$a is 2001:1250::12 and parent of $a is 2001:1250::/32 and data of parents is: 28497
$a is 2001:1250::12 and parent of $a is 2001:1250::/32 and data of parents is: 28497
$a is 2001:1250::12 and parent of $a is 2001:1250::/32 and data of parents is: 28497
$a is 2001:1250::12 and parent of $a is 2001:1250::/32 and data of parents is: 28497
$a is 2001:1250::12 and parent of $a is 2001:1250::/32 and data of parents is: 28497
$a is 2001:1250::12 and parent of $a is 2001:1250::/32 and data of parents is: 28497
$a is 2001:1250::12 and parent of $a is 2001:1250::/32 and data of parents is: 28497
$a is 2001:1250::12 and parent of $a is 2001:1250::/32 and data of parents is: 28497
$a is 2001:1250::12 and parent of $a is 2001:1250::/32 and data of parents is: 28497


FishMonger
Veteran / Moderator

Aug 6, 2010, 8:50 AM

Post #9 of 13 (1172 views)
Re: [AnkurMundhada] FOR LOOP HELP [In reply to] Can't Post

This question is double posted.
http://perlguru.com/gforum.cgi?post=47905


AnkurMundhada
Novice

Aug 8, 2010, 9:51 PM

Post #10 of 13 (1108 views)
Re: [FishMonger] FOR LOOP HELP [In reply to] Can't Post

HI,

If you can provide me the data files and a sample of your desired output, I'll can see if i can come up with some workable solution.
Thanks
Ankur M.


phone4u
Novice

Aug 10, 2010, 3:25 AM

Post #11 of 13 (1092 views)
Re: [AnkurMundhada] FOR LOOP HELP [In reply to] Can't Post

Please check your private mail, i have sent you the details and attach files. thanks


AnkurMundhada
Novice

Aug 10, 2010, 5:13 AM

Post #12 of 13 (1086 views)
Re: [phone4u] FOR LOOP HELP [In reply to] Can't Post

hi ,

We have to use two for loop to iterate through, to get the data value from the first log file.
I have made some modification in the code ,
1) once we got the match I am remove the array element from the array list. this can speed up the application by reducing each element from the array.
2) use "last" to restart the search from new line from "IPv6.txt".


Code
#!/usr/bin/perl 
use warnings;
use strict;

use Data::Dumper;
use Net::IPTrie;
my $tr = Net::IPTrie->new(version=>6); # IPv6
my %lexoAddHash = ();
my %lexoDataHash = ();

my @lexoDataHash = ();


open LEXO, "<", "lexo.txt" or die "Could not open file lexo.txt in read mode: $!\n";
my @lexo_content = <LEXO>;

foreach my $line (@lexo_content)
{
chomp $line;
if($line =~ m/address=>\'(.*?)\',\s*prefix=>\'(.*?)\',\s*data=>\'(.*?)\'/){
my $n = $tr->add(address=>"$1", prefix=>"$2", data=>"$3");
$lexoAddHash{$n->address} = $n->address;
push(@lexoDataHash,$3);
#$lexoDataHash{$n->address} = $3;
}
}
close LEXO;

print "Done 1\n";


open IPV6, "<", "IPv6.txt" or die "Could not open file IPv6.txt in read mode: $!\n";
while (<IPV6>) {
chomp $_;
for (my $i = 0 ; $i <= scalar @lexoDataHash ; $i++ ){
if(defined $lexoDataHash[$i]){
my $a = $tr->add(address => "$_" , prefix => '128', data => "$lexoDataHash[$i]");
if (defined $a->parent && defined $lexoAddHash{$a->parent->address} ) {
print "\$a is ". $a->address . " and parent of \$a is ". $a->parent->address ." and data of parents is: ". $a->parent->data. "\n";
splice(@lexoDataHash, $i, 1);
last;
}
}
}
}
close IPV6;

Thanks
Ankur M.


phone4u
Novice

Aug 10, 2010, 6:54 AM

Post #13 of 13 (1079 views)
Re: [AnkurMundhada] FOR LOOP HELP [In reply to] Can't Post

Hi, Thanks for your help....but it seems a bit slow in the output and the same problem of ignoring some IPs available on the list is still been experienced despite those address prefixes been inside lexo.txt. Just thought I should let you know....thanks alot.

 
 


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

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