http://qs321.pair.com?node_id=723846


in reply to Re: C-style for loop
in thread C-style for loop

No. You could write a C for loop as:

for ($_ = 0; $_ < @values; ++$_) { ... }

but that would be a particularly bad way to do it. A C for loop is generally a poorer solution that a Perl for loop because there are more parts to go wrong. A C style for loop is highly suceptible to off by one errors.


Perl reduces RSI - it saves typing

Replies are listed 'Best First'.
Re^3: C-style for loop
by ccn (Vicar) on Nov 15, 2008 at 21:05 UTC

    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.

      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:

      for my $index (0 .. $#array) { ... }

      Is generally a much better solution than:

      for (my $index = 0; $index < $#array; ++$index) { ... }

      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
        #!/usr/bin/perl -- use strict; use warnings; use Benchmark qw(cmpthese); my $BIG = 100_000; cmpthese(1000, { 'C-style' => \&c, 'P-style' => \&p}); sub c { for (my $i = $BIG; $i > 0; $i--) { 1; } } sub p { for my $i ( reverse 1..$BIG ) { 1; } } __END__ Rate P-style C-style P-style 26.7/s -- -44% C-style 48.0/s 80% --

        Is Perl-style still better?

      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:

      • first of all, the OP's post is ambiguous at best, with "this" being something that can mean quite about everything both general or very specific: I know he gives an actual example, but it may well be an actual example of what (s)he thinks to be a generic case;
      • the two loops even do different things, as you point out yourself in the first place!

      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.

      --
      If you can't understand the incipit, then please check the IPB Campaign.