loops

Reset loop, or return to start of loop, in Ruby? (Finding a number's factors)

I am trying to find the factors of a number N. I want to be able to iterate over my prime numbers array, and when a condition is met, go back to start of the iteration, rather than just continuing on. arrFactors = [] N = 150 [2,3,5,7,11].each do |a| if N % a == 0 arrFactors.push(a) N = N/a break if N == 1 <return t...

Registry problem - deleting key/values with C++

The following piece of code seems to unreliably execute and after and undeterministic time it will fail with error code 234 at the RegEnumValue function. I have not written this code, I am merely trying to debug it. I know there is an issue with doing RegEnumValue and then deleting keys in the while loop. I am trying to figure out f...

How do I add a delay in a JavaScript loop?

I would like to add a delay/sleep inside a while loop: I tried it like this: alert('hi'); for (var start = 1; start < 10; start++) setTimeout(function () { alert('hello'); }, 3000); Only the first scenario is true: after showing alert('hi'), it will be waiting for 3 seconds then alert('hello') will be displayed but then alert('...

PHP loop checking existence of a string in an array then outputting a boolean

Fairly rubbish with PHP, this is a continuation on from my last question. I have a list of user agents inside an array, and I want an output to be true if the user agent matches one listed inside the array. This is what I have for a single user agent: <?php function mediaType(){ $browser = strpos($_SERVER['HTTP_...

Placing a foreach loop inside an if condition

I am needing to create a foreach statement that will run through and create conditions for a conditional statement. I wrote this bit of code, not expecting it to work, and of course it didn't... $filename = "whitelist.txt"; $handle = fopen($filename, 'r'); $whitelist = fread($handle, filesize($filename)); fclose($handle); $whitelist = e...

Optimizing away a "while(1);" in C++0x

I have heard and read that C++0x allows an compiler to print "Hello" for the following snippet #include <iostream> int main() { while(1) ; std::cout << "Hello" << std::endl; } It apparently has something to do with threads and optimization capabilities. It looks to me that this can surprise many people though. Does someone...

Stuck with recursion within a loop

I am using recursion inside a loop. I want to stop the loop as soon as the method is called recursively and resume it again when the call returns. I am not able to do so. Do I need to lock the method? Here is my code: import java.lang.Thread; public class Max2Friends extends Thread { String[] f = {"NYNNN", "YNYNN", "NYNYN", "NNYNY...

How to add Or to a While Loop in VB?

Currently, lets say that I have this code: While r <> "HI" r = RandomStringGenerator(1) time = time + 1 Console.WriteLine(r) End While how can I make it so that i can say basically this: While r <> "HI" Or While r <> "BY" r = RandomStringGenerator(1) time = time + 1 Console...

Cupcake(API3) friendly way to pause/stop sound loops in SoundPool? Any ideas?

Quick note: I'm using the SoundPool class http://developer.android.com/reference/android/media/SoundPool.html What I have here is a simple button that plays a looped sound while it's pressed. It works great. However, sounds.autoPause(); wasn't introduced until API 8 and I really need something that is cupcake compatible (API 3) So i wa...

break statement ends the nested loop

Why does break statement ends the nested loop?it should get the iteration of the inner loop to stop while the outer loop should continue doing its work. int main() { for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { break; } } } This code breaks at the first iteration. ...

In Scala, how to stop reading lines from a file as soon as a criterion is accomplished?

Reading lines in a foreach loop, a function looks for a value by a key in a CSV-like structured text file. After a specific line is found, it is senseless to continue reading lines looking for something there. How to stop as there is no break statement in Scala? ...

How to make for loop in windows batch file run in name order

I have a windows batch file that does this: for %%s in (*.sql) do call It loops through all the sql script in a folder. In the folder the file names are like: s4.06.01.sql s4.07.01.sql s4.08.01.sql s4.10.01.sql s5.01.sql But the for loop goes through the files randomly (not in the name order), it first run s5.01, then s4.06, then s4...

jquery - take a set of hrefs from a list and apply those hrefs to another list in order

So for example, I have a list and I need to extract the hrefs from it: <ul class="list1"> <li><a href="link1">Link1</a></li> <li><a href="link2">Link2</a></li> <li><a href="link3">Link3</a></li> </ul> BTW, this forums spam protection is not letting me put any more hyper links which is why the code above is semantically incorrect. And...

How can we combine many Plots generated by a loop into one single Plot in Mathematica?

Dear all: I want to visualize a function of the form x^n such that n=1,2,...,10. I want these values of n to be generated by a loop and then plot the function as Plot[{x^n},{x,0,100}] and store the plot in an array. Then I want all of these arrays to be displayed into single plot. I tried to do this using Show function but with not much...

is there a loop controlled by time in c++?

Hi, I am wondering if I can loop x times in one minute interval between each loop. for (int x = 10; x > 0; x--) { cout << "BOOM" << endl; } Is there any way I can print boom every one minute? Or there is a better way to do this? Thank you ...

PHP Maths on SQL Data

Hello all, im not sure if this is php or mysql maths question. Ive got a table full of data, and although i can echo the individual data to the screen or echo and individual piece of data using php. I've no idea how to perform maths on the data itself. For simplicity, say my table has 4 columns, an id column and three with my data i wa...

dynamically add different strings into an array list base on a pattern/algorithm/formula

is there a way to dynamically add this whole adding process instead of the manually entering the values like a.Add("1 & 2"); and so on by the way, this only happens if i select 5 conditions -i am doing the adding base on the maximum condition i cater which is 5 the subsets that i show below must be in that pattern although its ok that ...

Difference between "while" loop and "do while" loop

What is the difference between while loop and do while loop. I used to think both are completely same.Then I came across following piece of code : do { printf("Word length... "); scanf("%d", &wdlen); } while(wdlen<2); This code works perfectly. It prints word length and tascans the input. But when I changed it to ...

Keeping track of current and previous MovieClip

Hi All, My code is doing what it is meant to be doing but somewhere along the line I am missing something in my logic. I have three movie clips. Each one of them have two frames. The first one has nothing on it (so it looks hidden) and the second frame has the animation on it. Using an array and the onEnterFrame function, I need to lo...

Ruby Loops Question

C++: for(i=0,j=0;i<0;i++,j++) What's the equivalence to this in ruby? Besides the normal for, while loop seen in C++. Can someone name off the other special loops ruby has? Such as .times? .each? Thanks in advance. ...