in reply to Finding the first element that passes a test - then saving the rest.

In your particular case just take advantage of the last statement e.g
my @found; for (0 .. $#array) { if ($array[$_] < 0 and $array[$_ - 1] > 0) { @found = @array[$_ .. $#array]; last; } }

HTH

_________
broquaint

  • Comment on Re: Finding the first element that passes a test - then saving the rest.
  • Download Code

Replies are listed 'Best First'.
Re: Re: Finding the first element that passes a test - then saving the rest.
by Not_a_Number (Prior) on Jun 05, 2003 at 23:02 UTC

    broquaint: this appears to fail for an array such as ( -1, -2, 10, -3, 2 ), because on the first iteration (when $_ == 0 ), $_ - 1 is the last element of the array:

    my @ary = qw ( a b c 2); print $ary[-1];

    Please correct (and downvote accordingly :-) if I've missed something

    dave