On Feb 6, Carl Rogers said:

>I'm using foreach to do something to an array. is there a way I can 
>reference a previously 'seen' element without having to traverse the array 
>with a for(;;) statement?
>
>ie. I'd like this to work:
>
>foreach(@array) {
>
>       if ($_ =~/somecondition/) {
>       # I want to do something to the element prior to $_ ($_ - 1)
>       }
>}

Just keep track of the previous element.

  my $is_first_iter = 1;
  my $prev_ele;

  for (@array) {
    if ($is_first_iter) {
      $prev_ele = $_;
      $is_first_iter = 0;
      next;
    }

    if (/condition/) {
      # fiddle with $prev_ele
    }

    $prev_ele = $_;
  }

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to