• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

Check this out !!

Is it possible to get out of a foreach loop, without stopping the processing the rest of the script ? I've tried it with the exit and last function, and I've tried it using a label..
These lines I have so far :

foreach(@velden){
($veld1,$veld2,$veld3)=split(/,/,$_);
if ($form{$veld1}){
#### The foreach loop must be stopped here ####
&goed;
}else{
print "error";
return vulin;
}}

sub goed {
print "allright";
}

As you can see the loop must be stopped at the given location. I must use an foreach-loop otherwise I've got to rewrite my whole script.
0
oostwijk
Asked:
oostwijk
  • 3
  • 3
1 Solution
 
maneshrCommented:
when you say "The foreach loop must be stopped here.." do you mean the processing should stop for a short time or the processing should end there.

if it is the later you should be fine with last.

0
 
oostwijkAuthor Commented:
well, the processing of the loop should stop, but the script must go on with sub goed. Give me the lines I should use..please.
0
 
maneshrCommented:
you dont need any thing extra at all!! the code should do what you want as it is right now.

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
oostwijkAuthor Commented:
Nope, it does call the sub goed, as many times there are items in the @field. The sub goed, has to be called only ones.
0
 
maneshrCommented:
let me get this straight now..

you have lets say 10 items in your array.

each time you check ..
if ($form{$veld1}){

if the above condition is true, you call goed.

Right???

now lets say of the 10 times that this loop is executed the if condition is true 4 times so goed will be executed 4 times.
But you want goed to be executed ONLY once, right???

if possible give me some raw data and also explain.
0
 
lambdaCommented:
put 'last' after &goed;


foreach(@velden){
($veld1,$veld2,$veld3)=split(/,/,$_);
if ($form{$veld1}){
#### The foreach loop must be stopped here ####
&goed;
last;
}else{
print "error";
return vulin;
}}

sub goed {
print "allright";
}
0
 
oostwijkAuthor Commented:
That does the job..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now