Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nice easy one! - How do I create an infinite loop??? 1

Status
Not open for further replies.

JaybOt

Programmer
Apr 18, 2001
101
GB
Hi all,

can anyone tell me (or post an example) how i can create an infinite loop? variations welcome.

Sorry for sounding a bit dunce, i have been programming for many years and in many languages but i am new to perl programming and the usual application of commands like 'do/while', 'case', and 'if/then/else' do not seem to work as expected in the shell.

any help would be appreciated!

JayBot
"Always know what you say, but don't always say what you know!"
 
1
Code:
    while (1) { print 1; }

2
Code:
    for(;;) { print 1; }

3
Code:
    {
       print 1;
       redo;
    }



 
Slight variation:

while (1) { next; }

better be careful though - that may tie up a lot(or most/all) of your cpu's resources. Would be better to put a sleep in there before the "next".
Hardy Merrill
Mission Critical Linux, Inc.
 
Cheers peeps!

Can anyone elaborate a bit more on how i can use these statements in my code?

would this be right...

while (1) {
print 1;
sleep;
do stuf }

while (1) {
print stuff;
sleep;
do stuf
next; }

Jaybot
"Always know what you say, but don't always say what you know!"
 
Sorry, when you invoke "sleep" you need to give it the number of seconds you want it to sleep, like

my $ct = 0;
while (1) {
$ct++;
print "$ct\n";
...do stuff...
sleep 5; ### sleeps for 5 seconds
}

That is an infinite loop - to make it stop, you'll either have to kill the process manually, or put a condition in there that will do a "last" or "exit" to break out of the loop.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
I have re-writen the above script and it now contins another loop within an 'if' statement. How can i exit this loop, returning control to the original loop.

here is the code (simplified!)

#!/usr/bin/perl
while(1){
if (condition){
do stuff
while(1){
if (condition2){
do more stuff
exit 0
}else{
do nothing but loop2
}
}
}else{
sleep sometime
}
}

the original loop/if works o.k. But the second loop will not exit at the 'exit 0' line. I just want to return to the original loop.

Any idears welcome!

Regards,
JayBot!

"Always know what you say, but don't always say what you know!"
 
I don't know what you want to do but if there's some way that your program can tell if it is done with a loop, then you can do something like this:

#!/usr/bin/perl

$doloop = 1;
$sleeplen = 2;

# loop until $doloop becomes 0
while ($doloop) {
# do stuff
sleep $sleeplen;
if (some condition) {
$doloop = 0;
}
}

This is essentially creating a variable to indicate the status of the program. you could run a test for a specific value of $doloop, and have it loop until it is some value and indicate 0 or some number to be the exit status. This will allow you to also diagnose your program better because you can return a message based on the status value and know where your error was.
Steve Kiehl
webmaster@nanovox.com
 
also, the command 'last' will exit the innermost loop. "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top