The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Multiple foreach statements
Discuss Multiple foreach statements in the Perl Programming forum on Dev Shed. Multiple foreach statements Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 14th, 2012, 05:39 AM
|
|
Contributing User
|
|
Join Date: Jul 2012
Posts: 64
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
|

August 14th, 2012, 05:43 AM
|
 |
kill 9, $$;
|
|
Join Date: Sep 2001
Location: Shanghai, An tSín
|
|
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)?
|

August 14th, 2012, 06:04 AM
|
|
|
|
Yes, you can have multiple nested foreach loops.
|

August 14th, 2012, 06:05 AM
|
|
Contributing User
|
|
Join Date: Jul 2012
Posts: 64
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)? |
|

August 14th, 2012, 06:49 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 3
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";
}
|

August 14th, 2012, 08:44 AM
|
|
|
|
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.
|

August 14th, 2012, 09:39 AM
|
|
Contributing User
|
|
Join Date: Jul 2012
Posts: 64
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";
}
|
|

August 14th, 2012, 09:50 AM
|
|
|
|

August 14th, 2012, 12:58 PM
|
|
|
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 |
|
|
|
foreach $source (@SourceList) {
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 |
|
|
|
foreach my $source (@SourceList) {
The following line:
Perl Code:
Original
- Perl Code |
|
|
|
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 |
|
|
|
$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.
|

August 15th, 2012, 03:33 AM
|
|
Contributing User
|
|
Join Date: Jul 2012
Posts: 64
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 |
|
|
|
foreach $source (@SourceList) {
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 |
|
|
|
foreach my $source (@SourceList) {
The following line:
Perl Code:
Original
- Perl Code |
|
|
|
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 |
|
|
|
$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. |
|

August 15th, 2012, 08:28 AM
|
|
|
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
|

August 15th, 2012, 08:39 AM
|
|
|
|
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.
|

August 15th, 2012, 09:46 AM
|
|
Contributing User
|
|
Join Date: Jul 2012
Posts: 64
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>;
|

August 15th, 2012, 09:55 AM
|
|
|
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;
|

August 15th, 2012, 10:06 AM
|
|
Contributing User
|
|
Join Date: Jul 2012
Posts: 64
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;
|
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|