in reply to Re^2: C-style for loop
in thread C-style for loop
Yes, one can use $_ instead of $i, but I talked about the loop specified by OP. And if in theory we can't say what type of a loop is better but in specific case we can.
Unfortunately OP gave not equivalent loops. The first one modifies original array and the second one creates new array with modified data. I rewrite in perl-style the first loop.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: C-style for loop
by GrandFather (Saint) on Nov 15, 2008 at 21:26 UTC | |
I gave you a reason for preferring the Perl style for loop in general - fewer parts to go wrong. Eliminating a variable is not generally the important difference between two different for loop usages. Indeed:
Is generally a much better solution than:
because the range of the Perl loop is much easier to see and harder to get wrong. In general we can say what sort of for loop is better - Perl for loops. '"C-style" is bad because of useless $i variable' is not the reason however. Perl reduces RSI - it saves typing | [reply] [d/l] [select] |
by ccn (Vicar) on Nov 15, 2008 at 22:01 UTC | |
Is Perl-style still better? | [reply] [d/l] |
by GrandFather (Saint) on Nov 15, 2008 at 23:08 UTC | |
Absolutely if the point you are trying to make is that there is a tiny execution penalty in this rather artificial case for using the Perl style for loop. An interesting test is to change the loops from reverse to forwards and see where the differences lie. For the C style for loop you need to change every part of the loop header in a major way and you have to think a while to ensure the range is what you want:
For the Perl loop just deleting the reverse does the trick and the range is still completely clear. Oh, and now the Perl loop overhead is less than the C loop overhead:
Considering how small the overhead of using reverse is and how much more maintainable the Perl loop is you have done an admirable job of demonstrating why in the general case the Perl for loop is much better than the C style for loop. Perl reduces RSI - it saves typing | [reply] [d/l] [select] |
by ikegami (Patriarch) on Nov 16, 2008 at 01:42 UTC | |
So far, I've found 6 different kinds of for loops in Perl. You can find them in an earlier node.
If you add "()," to the front of any of the lists, you'll notice that p_* will become as slow as p_rv. | [reply] [d/l] [select] |
by zwon (Abbot) on Nov 15, 2008 at 23:00 UTC | |
| [reply] [d/l] |
Re^4: C-style for loop
by blazar (Canon) on Nov 18, 2008 at 14:28 UTC | |
Unfortunately OP gave not equivalent loops. The first one modifies original array and the second one creates new array with modified data. I rewrite in perl-style the first loop. I personally believe, ccn, that I can understand your point. More precisely, you also dropped a /msg to me, which I'm reproducing hereafter - I hope it's not a problem: Re Re^2: C-style for loop OMG! Please look at OP question. He asks about specific "this" loop, not general. I answered about that loop, not general! May be I said not what I actually mean. Not $i but *index* is useless there, coz we can iterate without it But then please let me get straight and direct and stress what quite about everyone is trying to say here: In view of all this, you did not "rewrite in perl-style the first loop:" but as a statement modifier, which is something still slightly different, albeit certainly more Perl-stylish than the C-style form of C<for> loop. | [reply] [d/l] [select] |