Solved

Check this out !!

Posted on 2000-04-04
7
150 Views
Last Modified: 2013-12-25
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
Comment
Question by:oostwijk
  • 3
  • 3
7 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2685442
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
 

Author Comment

by:oostwijk
ID: 2685567
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
 
LVL 16

Expert Comment

by:maneshr
ID: 2685656
you dont need any thing extra at all!! the code should do what you want as it is right now.

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:oostwijk
ID: 2685673
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
 
LVL 16

Expert Comment

by:maneshr
ID: 2685706
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
 
LVL 1

Accepted Solution

by:
lambda earned 45 total points
ID: 2686237
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
 

Author Comment

by:oostwijk
ID: 2687956
That does the job..
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now