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: Advanced:
A mysterious question regarding if last....:)

 



yanivr78
Novice

Mar 10, 2013, 12:08 PM

Post #1 of 2 (4142 views)
A mysterious question regarding if last....:) 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 (not sure what this number 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



Kenosis
User

Mar 10, 2013, 1:05 PM

Post #2 of 2 (4138 views)
Re: [yanivr78] A mysterious question regarding if last....:) [In reply to] Can't Post

This posting has been addressed in this forum.

 
 


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

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