Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    68
    Rep Power
    3

    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
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    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)?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    Yes, you can have multiple nested foreach loops.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    68
    Rep Power
    3
    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?


    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)?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    3
    Rep 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";
    }
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,971
    Rep Power
    1225
    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
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    68
    Rep Power
    3
    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!


    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";
    }
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,971
    Rep Power
    1225
    So, you just need to remove the duplicate entries?

    FAQ: How can I remove duplicate elements from a list or array?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    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:
    foreach $source (@SourceList) 
    {
           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:
    foreach my $source (@SourceList) 
    {
           chomp $source;


    The following line:

    Perl Code:
    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:
    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
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    68
    Rep Power
    3
    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!


    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:
    foreach $source (@SourceList) 
    {
           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:
    foreach my $source (@SourceList) 
    {
           chomp $source;


    The following line:

    Perl Code:
    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:
    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.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,971
    Rep Power
    1225
    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
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    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.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    68
    Rep Power
    3
    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>;
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,971
    Rep Power
    1225
    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;
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    68
    Rep Power
    3
    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"; 		
       <>; 	
    }

    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;
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo