in reply to 'each' syntax oddity?

for (my $i = 0; $i < $#array; $i+=2) { my ($first, $second) = @array[$i, $i+1]; # Do stuff here ... }

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

  • Comment on Why do Perlers hate C-style for-loops, especially when they're called for?
  • Download Code

Replies are listed 'Best First'.
•Re: Why do Perlers hate C-style for-loops, especially when they're called for?
by merlyn (Sage) on Mar 07, 2002 at 15:03 UTC
    Why do some people speak Perl with a C accent? {grin} A more native Perl solution:
    my @copy = @array; while (my ($first, $second) = splice @copy, 0, 2) { # do stuff }
    Your code has many potential "off by one" locations. Yes, it's possible to get it exactly right, but much safer to let Perl deal with the edges than for me to keep computing the edges. Call it defensive programming.

    -- Randal L. Schwartz, Perl hacker