in reply to Getting the next array element while still keeping the current one
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] ); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getting the next array element while still keeping the current one
by Anonymous Monk on May 01, 2004 at 20:33 UTC | |
by tkil (Monk) on May 01, 2004 at 20:55 UTC | |
by hv (Prior) on May 02, 2004 at 12:28 UTC | |
by tkil (Monk) on May 02, 2004 at 18:07 UTC |