in reply to Style and Preference on Loops

I use a mix of both. If I need to know the actual index in the loop for something I'm doing, the first works. If I'm just iterating over the elements and don't necessarily need to know the index, I use the second.

Replies are listed 'Best First'.
Re^2: Style and Preference on Loops
by chromatic (Archbishop) on Aug 12, 2011 at 17:43 UTC

    Perl 5.12 added the capability for each to return the index and value of an array in list context.

Re^2: Style and Preference on Loops
by GrandFather (Saint) on Aug 12, 2011 at 23:24 UTC

    If you need to know the index and are using an increment of 1 then:

    for my $index (0 .. $#$mysql) {

    is much better than the C for loop variant. About the only time a C loop is likely to be used in Perl is when the "increment" is not 1 or the termination condition is not simply reaching an end point, although there are often better ways of achieving both objectives than using a C for loop.

    Update: $#$mysql per eyepopslikeamosquito's catch

    True laziness is hard work