in reply to Re: Looping backwards through two arrays at once?
in thread Looping backwards through two arrays at once?

I wasn't aware of for ( reverse N .. M ) being optimized in *any* case. Because of that, I'd normally write that without the reverse() and use the number as an offset from the end. In the loop, treating the offset as a negative number works to get the "from the end" effect. This buys the iterator space savings of for(N..M) back.

for my $offset ( 0 .. $#ary_a ) { foobar( $ary_a[ -$offset ], $ary_b[ -$offset ] ); }

Replies are listed 'Best First'.
Re^3: Looping backwards through two arrays at once?
by duff (Parson) on Nov 04, 2005 at 17:56 UTC
    for my $offset ( 0 .. $#ary_a ) { foobar( $ary_a[ -$offset ], $ary_b[ -$offset ] ); }

    This is the reply I was going to give but you're suffering from an off-by-one error. It should be:

    for my $offset (1..@ary_a) { foobar( $ary_a[ -$offset ], $ary_b[ -$offset ] ); }