CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Quick question regarding next/last and redo

 



yanivr78
Novice

Mar 10, 2013, 12:01 PM

Post #1 of 5 (651 views)
Quick question regarding next/last and redo Can't Post

Hi all,

I would like to ask about the logic of this script :


Code
 $i = 0; 
while ( 1 ) {
last if $i > 3;
$i++;
next if $i == 1;
redo if $i == 2;
}
continue {
print "$i\n";
}


let me translate it to words :
1) scalar i = zero

2) while (1) (not sure what does the number 1 stands for - can anyone explain ?)

3) raise scalar I by 1 (since it starts from 0 it is now "1" inside the first iteration).

4) this condition is met : "If scalar I = 1 then go to the end of the code (i.e print the number "1") and then restart back to the beginning of the while block code again.


END OF FIRST ITERATION

BEGINNING OF NEXT ITERATION :

6) now when scalar i = 2 it meets redo,
it goes back to the beginning of the code block while avoiding printing "2".

END OF 2nd ITERATION

BEGINNING OF 3rd ITERATION

7) scalar i starts again, and becomes "3" - (it avoids last if $i > 3; before becoming 2 - right?) and will be printed. then it will go back to the beginning of the while code block.

8) now comes the hard part...ready ?
the scalar is now "3" right ?, so "last if" should block it...
unfortunately it doesn't... Can anyone solve this mystery ?

Many thanks for any answer!

P.S - here's the output :

Code
1 
3
4



(This post was edited by yanivr78 on Mar 10, 2013, 12:38 PM)


Chris Charley
User

Mar 10, 2013, 12:47 PM

Post #2 of 5 (641 views)
Re: [yanivr78] Quick question regarding next/last and redo [In reply to] Can't Post


Code
the scalar is now "3" right ?, so "last if" should block it...

The if checks for a value greater than 3, not equal to 3.

Here is the documentation on next, last and redo.

Code
C:\Old_Data\perlp>perldoc -f continue 
continue BLOCK
continue
"continue" is actually a flow control statement rather than a
function. If there is a "continue" BLOCK attached to a BLOCK
(typically in a "while" or "foreach"), it is always executed
just before the conditional is about to be evaluated again, just
like the third part of a "for" loop in C. Thus it can be used to
increment a loop variable, even when the loop has been continued
via the "next" statement (which is similar to the C "continue"
statement).

"last", "next", or "redo" may appear within a "continue" block;
"last" and "redo" behave as if they had been executed within the
main block. So will "next", but since it will execute a
"continue" block, it may be more entertaining.

while (EXPR) {
### redo always comes here
do_something;
} continue {
### next always comes here
do_something_else;
# then back the top to re-check EXPR
}
### last always comes here

Omitting the "continue" section is equivalent to using an empty
one, logically enough, so "next" goes directly back to check the
condition at the top of the loop.

If the "switch" feature is enabled, "continue" is also a
function that falls through the current "when" or "default"
block instead of iterating a dynamically enclosing "foreach" or
exiting a lexically enclosing "given". See feature and "Switch
statements" in perlsyn for more information.



Kenosis
User

Mar 10, 2013, 1:03 PM

Post #3 of 5 (638 views)
Re: [yanivr78] Quick question regarding next/last and redo [In reply to] Can't Post


Quote
2) while (1) (not sure what does the number 1 stands for - can anyone explain ?)


Chris Charley showed the use of while:

Code
while (EXPR) { ...


As long as EXPRession evaluates to true, the while loop will continue executing. In your script, "while(1) {... " creates an infinite loop, since 1 is always true. The last statement is used to break out of that infinite loop.


yanivr78
Novice

Mar 10, 2013, 1:32 PM

Post #4 of 5 (633 views)
Re: [Kenosis] Quick question regarding next/last and redo [In reply to] Can't Post

Thanks guys !
that answer why the output prints 4 and stops.


Rahul6990
Novice

Mar 12, 2013, 2:46 AM

Post #5 of 5 (616 views)
Re: [yanivr78] Quick question regarding next/last and redo [In reply to] Can't Post

Next: Go to next loop iteration
Last: Came out of the loop
Redo: Restart the loop

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives