in reply to best way to implement multiple foreach iterator variables?

my $cur_index = 0; while ( $cur_index <= $#x - 1 ) { my ($x, $y) = @x[$cur_index .. $cur_index + 1]; $cur_index += 2; # Do stuff with $x and $y }

If you want the aliasing capabilities, look into Data::Alias.

As for the neat P6 syntax (for @x -> $x, $y { ... }), you'll be needing a source filter for that.


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re^2: best way to implement multiple foreach iterator variables?
by adrianh (Chancellor) on Feb 19, 2006 at 14:34 UTC

    I like List::MoreUtils natatime for this sort of task:

    use List::MoreUtils qw( natatime ); my $next_two = natatime 2, @x; while ( my ( $x, $y ) = $next_two->() ) { # do something with $x & y };