in reply to Increment by 2 and by 1 in same loop.

Like this:

my $x=0; my $size=5; for( my $i=0; $i<$size; $i+=2,$x++ ) { print "$i:$x\n"; }

or

my $x=0; my $size=5; for( my $i=0; $i<$size; $i+=2 ) { print "$i:",$x++,"\n"; }

Replies are listed 'Best First'.
Re^2: Increment by 2 and by 1 in same loop.
by AnomalousMonk (Archbishop) on Apr 16, 2013 at 10:21 UTC

    It's also possible to define (and initialize, if needed) two (or more) lexical loop variables entirely locally:

    for (my ($i, $n) = (1, 2); $i < $limit; $i += 2, ++$n) { do_something_with($i, $n); }