Home: Perl Programming Help: Advanced:
A mysterious question regarding if last....:)



yanivr78
Novice

Mar 10, 2013, 12:08 PM


Views: 7608
A mysterious question regarding if last....:)

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


Views: 7604
Re: [yanivr78] A mysterious question regarding if last....:)

This posting has been addressed in this forum.