in reply to Re: Deleting specific element in array in FOREACH loop
in thread Deleting specific element in array in FOREACH loop

for my $index (reverse 0 .. $#array)

is a more Perlish way to achieve that. It is clearer and avoids fence posts.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^3: Deleting specific element in array in FOREACH loop
by ikegami (Patriarch) on Sep 15, 2006 at 20:08 UTC

    The downside is that
    for my $index (reverse 0 .. $#array)
    creates a list as large as the array, whereas
    for (my $i = $#array; $i > -1; $i--)
    and
    for (my $i = @array; $i--; )
    have no memory cost. That said, it usually doesn't matter.

    On the plus side,
    for my $index (reverse 0 .. $#array)
    could be slower. It is optimized to loop backwards instead of actually calling reverse.

      for modern versions of perl, this is no longer true for normal ranges. does it still create the array for 'reverse' ranges?