On Sunday 10 October 2010 03:09:21 Ron Bergin wrote:
> On Oct 7, 3:07 pm, shlo...@iglu.org.il (Shlomi Fish) wrote:
> > Hi all,
> > 
> > after being tired of telling Perl newcomers about the same problems with
> > their code times and times again, I've decided to create this page
> > detailing "Perl Elements to avoid":
> > 
> > http://perl-begin.org/tutorials/bad-elements/
> 
> [quote]
> C-style for loops
> 
> Some beginners to Perl tend to use C-style-for-loops to loop over an
> array's elements:
> [/quote]
> 
> Have you considered including an example were it would be appropriate
> to use the C-style for loop, such as when the iterator needs to change
> by some value other than 1?

Well, in this case, PBP recommends to use a while/continue loop:

[code]
for (my $i = 0; $i < $n ; $i += 2)
{
        print "$i\n";
}
[/code]

into:

[code]
{
        my $i = 0;
        
        while ($i < $n)
        {
                print "$i\n";
        }
        continue
        {
                $i += 2;
        }
}
[/code]

But it's a good idea to show both versions. Thanks!

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Optimising Code for Speed - http://shlom.in/optimise

<rindolf> She's a hot chick. But she smokes.
<go|dfish> She can smoke as long as she's smokin'.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to