You're very close. The post-increment operator (as in $i++) returns the current value and then increments the value, while the pre-increment operator (as in ++$i), increments the value immediately and returns the incremented value.
In this case, however, I don't think you even want that, as it will mess up your counter. All you really want is $i + 1 (untested code follows, note that this will mess up on the last output, since nothing follows 'betty'):
my @array = qw( wilma fred barney betty ); for my $i (0 .. $#array) { printf( "We have %s, while %s is next.", $array[$i], $array[$i + 1] ); }
In reply to Re: Getting the next array element while still keeping the current one
by Anonymous Monk
in thread Getting the next array element while still keeping the current one
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |