Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 14th, 2012, 05:39 AM
andreas.london andreas.london is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 64 andreas.london User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 6 m 55 sec
Reputation Power: 1
Multiple foreach statements

hi guys,

does anyone knows if i can have multiple foreach statements in my script? I mean one foreach in the other one
e.g:

foreach ....()
{
.................
.................
foreach ....()
{
...............
}

}

the problem that i have is that when the small foreach is done and goes back to the big foreach to execute the commands and go again to the small foreach, the foreach starts from the line that ends before.

how can i fix it?


thanks

Reply With Quote
  #2  
Old August 14th, 2012, 05:43 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Sep 2001
Location: Shanghai, An tSín
Posts: 6,894 ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 36 m 34 sec
Reputation Power: 3885
Quote:
Originally Posted by andreas.london
the problem that i have is that when the small foreach is done and goes back to the big foreach to execute the commands and go again to the small foreach, the foreach starts from the line that ends before.

I don't understand what you mean by this. Do you have sample code that shows this issue (and explain what it's doing that's different to what you wanted it to do)?

Reply With Quote
  #3  
Old August 14th, 2012, 06:04 AM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 508 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 29 m 2 sec
Reputation Power: 385
Yes, you can have multiple nested foreach loops.

Reply With Quote
  #4  
Old August 14th, 2012, 06:05 AM
andreas.london andreas.london is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 64 andreas.london User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 6 m 55 sec
Reputation Power: 1
i have source.txt with the following entries:
Gi2/0/9.2502,2502,89.200.128.95,958401,UP,Kingsmead SSKMD,UP,sr0.enbrs.ov.easynet.net,235501
Gi2/0/9.2503,2503,89.200.128.93,938401,UP,THWO,UP,sr0.enslo.ov.easynet.net,235502
Gi2/0/9.2504,2504,84.38.34.183,1088401,UP,MRMID,UP,sr10.pomnc.isp.sky.com,235503
Gi2/0/9.2505,2505,84.38.34.92,858403,UP,CLCLE,UP,sr11.bllon.isp.sky.com,235504
Gi2/0/9.2506,2506,84.38.34.179,858405,UP,CLSOU,UP,sr10.bllon.isp.sky.com,235505
Gi2/0/9.2507,2507,84.38.34.92,858407,UP,WRCHEL,UP,sr11.bllon.isp.sky.com,235506
Gi2/0/9.2508,2508,84.38.34.92,818403,UP,CLHOL,UP,sr11.bllon.isp.sky.com,235507

after the script read the first line it splits the data after the "," to multiple variables.
then i'm getting the ip address and i want to check if already exist in different text file, and if not write the ip address in the txt file.

Code:
foreach $source (@SourceList) 
{
       chomp my $source;
       foreach $ip (@IPLIST)
       {
               chomp $ip;
               
               if  ($source eq $ip)
               {
                     $count = 0;
                     last;
               }

               elsif (($source ne $ip)
               {
                       $count = 1;
               }
       }

       if ($count == 1)
       {
              print MYIP "$ip\n";
       }
}


and is not working, it still write the existing entries as well in the ip.txt

make sense?


Quote:
Originally Posted by ishnid
I don't understand what you mean by this. Do you have sample code that shows this issue (and explain what it's doing that's different to what you wanted it to do)?

Reply With Quote
  #5  
Old August 14th, 2012, 06:49 AM
tnedor tnedor is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 3 tnedor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 34 m 34 sec
Reputation Power: 0
Note sure if I understand what you're trying to achieve here, but try something like this:

Code:
#!/usr/bin/perl -w
use strict;
use warnings;

my @SourceList = (
    "89.200.128.95",
    "89.200.128.93",
    "84.38.34.183",
    "84.38.34.92",
    "84.38.34.179",
    "84.38.34.92",
    "84.38.34.92"
);

my $count = 0;

my @IPLIST = (
    "89.200.128.95",
    "89.200.128.93",
    "84.38.34.183"
);

foreach my $source (@SourceList)
{
    chomp $source;
    
    foreach my $ip (@IPLIST)
    {
        chomp $ip;
        
        if ($source eq $ip)
        {
            $count = 0;
            last;
        }
        
        elsif ($source ne $ip)
        {
            $count = 1;
        }
    }
    
    if ($count)
    {
        push (@IPLIST, $source);
    }
}

foreach my $pr (@IPLIST)
{
    print $pr , "\n";
}

Reply With Quote
  #6  
Old August 14th, 2012, 08:44 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,650 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 2 h 37 m 31 sec
Reputation Power: 1170
Instead of nested foreach loops, it would be more efficient to load the list of IP's into a hash and then as you loop over the csv data, do a hash lookup to see if the IP is in the hash.
Comments on this post
ishnid agrees!

Reply With Quote
  #7  
Old August 14th, 2012, 09:39 AM
andreas.london andreas.london is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 64 andreas.london User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 6 m 55 sec
Reputation Power: 1
i dont think that this will solve my problem.
basically i will make it more clear.
i have a text file with many entries, which some of them are duplicate, (e.g the same entry might be 10 times in the text file)
and i want to write in another txt the entries from the beginning but the duplicate entries should be written once in the new file.

any idea?
Thanks!


Quote:
Originally Posted by tnedor
Note sure if I understand what you're trying to achieve here, but try something like this:

Code:
#!/usr/bin/perl -w
use strict;
use warnings;

my @SourceList = (
    "89.200.128.95",
    "89.200.128.93",
    "84.38.34.183",
    "84.38.34.92",
    "84.38.34.179",
    "84.38.34.92",
    "84.38.34.92"
);

my $count = 0;

my @IPLIST = (
    "89.200.128.95",
    "89.200.128.93",
    "84.38.34.183"
);

foreach my $source (@SourceList)
{
    chomp $source;
    
    foreach my $ip (@IPLIST)
    {
        chomp $ip;
        
        if ($source eq $ip)
        {
            $count = 0;
            last;
        }
        
        elsif ($source ne $ip)
        {
            $count = 1;
        }
    }
    
    if ($count)
    {
        push (@IPLIST, $source);
    }
}

foreach my $pr (@IPLIST)
{
    print $pr , "\n";
}

Reply With Quote
  #8  
Old August 14th, 2012, 09:50 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,650 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 2 h 37 m 31 sec
Reputation Power: 1170
So, you just need to remove the duplicate entries?

FAQ: How can I remove duplicate elements from a list or array?

Reply With Quote
  #9  
Old August 14th, 2012, 12:58 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 508 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 29 m 2 sec
Reputation Power: 385
Yes, I agree totally with what has been said, use a hash for finding duplicates, this is far better. Depending on what you want to do exactly (which I don't really know because it seems different from one of your post to the next), a "grep" may be better than the "foreach" loop as it would return directly a list of IP satisfying one conditions.

Yet, I still would like to point out some errors in the code you posted to help you understand these mistakes:

Perl Code:
Original - Perl Code
  1. foreach $source (@SourceList)
  2. {
  3.        chomp my $source;


Remove the "my" in the last line above. $source becomes undefined, it no longer hold the value set in foreach line. And BTW, the foreach instruction would be better written:

Perl Code:
Original - Perl Code
  1. foreach my $source (@SourceList)
  2. {
  3.        chomp $source;


The following line:

Perl Code:
Original - Perl Code
  1. foreach $ip (@IPLIST)


assignss successively a value to $ip for the duration of the block, i.e. until the closing curly brace.

In other words, when you do

Perl Code:
Original - Perl Code
  1. print MYIP "$ip\n";


$ip is no longer defined (it has fallen out of scope).

You must have had scores of "Use of uninitialized value ..." in the output when you tried running the program.

Also, if you had:

Code:
use strict;
use warnings;


some of these errors would probably have been detected.
Comments on this post
ishnid agrees!

Reply With Quote
  #10  
Old August 15th, 2012, 03:33 AM
andreas.london andreas.london is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 64 andreas.london User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 6 m 55 sec
Reputation Power: 1
thanks guys for your help. i do have another question.
during the loop proccess i come up with a result which i save it in a text file. when the script goes back again to the loop i want to open the txt file but the new one with the value that i just saved in. Any ideas how can i do that?

thanks again!


Quote:
Originally Posted by Laurent_R
Yes, I agree totally with what has been said, use a hash for finding duplicates, this is far better. Depending on what you want to do exactly (which I don't really know because it seems different from one of your post to the next), a "grep" may be better than the "foreach" loop as it would return directly a list of IP satisfying one conditions.

Yet, I still would like to point out some errors in the code you posted to help you understand these mistakes:

Perl Code:
Original - Perl Code
  1. foreach $source (@SourceList)
  2. {
  3.        chomp my $source;


Remove the "my" in the last line above. $source becomes undefined, it no longer hold the value set in foreach line. And BTW, the foreach instruction would be better written:

Perl Code:
Original - Perl Code
  1. foreach my $source (@SourceList)
  2. {
  3.        chomp $source;


The following line:

Perl Code:
Original - Perl Code
  1. foreach $ip (@IPLIST)


assignss successively a value to $ip for the duration of the block, i.e. until the closing curly brace.

In other words, when you do

Perl Code:
Original - Perl Code
  1. print MYIP "$ip\n";


$ip is no longer defined (it has fallen out of scope).

You must have had scores of "Use of uninitialized value ..." in the output when you tried running the program.

Also, if you had:

Code:
use strict;
use warnings;


some of these errors would probably have been detected.

Reply With Quote
  #11  
Old August 15th, 2012, 08:28 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,650 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 2 h 37 m 31 sec
Reputation Power: 1170
Since you already know how to open a file, your question doesn't make much sense, but I'll answer it by asking you to read this: perldoc -f open

Reply With Quote
  #12  
Old August 15th, 2012, 08:39 AM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 508 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 29 m 2 sec
Reputation Power: 385
If you want to delete what is in the file and write new contents, open the file in the ">" mode. If you want to append the new contents to the existing contents, use the ">>" mode.

Reply With Quote
  #13  
Old August 15th, 2012, 09:46 AM
andreas.london andreas.london is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 64 andreas.london User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 6 m 55 sec
Reputation Power: 1
basically when i try to open a file inside the foreach loop is not working (not opening the file).
the script below is in the second foreach loop (it means that i have second foreach loop inside the first one)

Code:
$peer_add = "C:/Users/TsentisA/Documents/Project/peer_address.txt";     
open(PEER, $peer_add);     
@PeerList = <PEER>;

Reply With Quote
  #14  
Old August 15th, 2012, 09:55 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,650 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 2 h 37 m 31 sec
Reputation Power: 1170
Code:
$peer_add = "C:/Users/TsentisA/Documents/Project/peer_address.txt";     
open(my $peer_fh, '<', $peer_add) or die "failed to open '$peer_add' $!";     
@PeerList = <$peer_fh>;
close $peer_fh;

Reply With Quote
  #15  
Old August 15th, 2012, 10:06 AM
andreas.london andreas.london is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 64 andreas.london User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 6 m 55 sec
Reputation Power: 1
not working at all. it doesn't print all the the entries of the file.

Code:
$peer_add = "C:/Users/TsentisA/Documents/Project/peer_address.txt";          
open(my $peer_fh, '<', $peer_add) or die "failed to open '$peer_add' $!";          
@PeerList = <$peer_fh>;     
close $peer_fh; 	 	
	 	
foreach $andreas (@PeerList) 	
{ 		
   chomp $andreas; 		
   print "$andreas\n"; 		
   <>; 	
}



Quote:
Originally Posted by FishMonger
Code:
$peer_add = "C:/Users/TsentisA/Documents/Project/peer_address.txt";     
open(my $peer_fh, '<', $peer_add) or die "failed to open '$peer_add' $!";     
@PeerList = <$peer_fh>;
close $peer_fh;

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Multiple foreach statements

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap