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: Edit Log



yanivr78
Novice

Mar 10, 2013, 12:01 PM


Views: 608
Quick question regarding next/last and redo

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)


Edit Log:
Post edited by yanivr78 (Novice) on Mar 10, 2013, 12:02 PM
Post edited by yanivr78 (Novice) on Mar 10, 2013, 12:05 PM
Post edited by yanivr78 (Novice) on Mar 10, 2013, 12:07 PM
Post edited by yanivr78 (Novice) on Mar 10, 2013, 12:38 PM


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

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