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

Just an outlined hint, but you could use recursion and Perl's ability to store the returned list in separate variables.
use strict; use warnings; sub beaver_find { my ($cond, @l) = @_; return () unless @l; my $head = shift @l; return $cond->( $head ) ? ($head, @l) : beaver_find( $cond, @l ); } my $cond = sub { my $x = shift; return $x > 0 }; my @l = ( -1, -3, -2, 10, -1, -3, -2 ); my( $head, @tail ) = beaver_find( $cond, @l ); print qq| Head: $head Tail: @tail |;
  • Comment on Re: Finding the first element that passes a test - then saving the rest.
  • Download Code