C-style loops vs Perl loops (in Perl) -
I think there is something that I do not get about Pearl's looping system.
It was my understanding that for <$ p>
my $ j (0 .. $ # arr) {...}
Functionally equivalent to:
for (my $ i = 0; $ i & lt; = $ # arr; $ i ++) {..}
However, my code seems to have some minor differences in their work. Specifically, the time in which they decide when to end. For example:
Suppose @arr has started with a variable in it.
Should both these blocks fix the same thing?
For my $ i (0 .. $ # arr) {if (some conditions that come true) {push (@arr, $ value); }} For
and
(my $ i = 0; $ i & lt; = $ # arr; $ i ++) {if (Some conditions may be true) {push (@arr, $ value); }}
Although in execution, although a new value is pushed into both positions, the first loop will be closed only after one recurrence.
Should this happen? If so, why?
Edit: Thank you for all your replies, I know that I can fulfill the same thing with other looping mechanisms. When I asked if there is another syntax, I was specifically talking about using it for it. Obviously there is not. As for who I want Syntax already achieved with C style I was only asking because I was asked to avoid C style but I still like to loop.
$ i & lt; = $ # Arr
is evaluated before each loop, whereas (0 .. $ # arr)
is evaluated once before any loop.
In this way, the first code @arr
.
Comments
Post a Comment