# Verify $prev is NON-empty
if ($prev) { ... }
####
# straightforward approach: track whether this
# is the first iteration or not explicitly.
my $is_first = 1;
my $prev;
foreach my $elem ( @array )
{
if ( $is_first )
{
$is_first = 0;
}
else
{
push @result, compute( $elem, $prev );
}
$prev = $elem;
}
####
# note that this alters @array
my $prev = shift @array;
foreach my $elem ( @array )
{
push @result, compute( $elem, $prev );
$prev = $elem;
}
####
my ( $prev, @rest ) = @array;
foreach my $elem ( @rest )
{
push @result, compute( $elem, $prev );
$prev = $elem;
}
####
my $prev = $array[0];
foreach my $elem ( @array[ 1 .. $#array ] )
{
push @result, compute( $elem, $prev );
$prev = $elem;
}
####
# note that we want $i to stop one short of $#array,
# since we access $array[$i+1].
for ( my $i = 0; $i < $#array; ++$i )
{
push @result, compute( $array[$i+1], $array[$i] );
}