Help for this page

Select Code to Download


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