As other answers show grep, usually everything you need.
However, using a perl function prototypes , you can use code that, like ruby Array#reject!:
- takes a block
- can change its array of arguments in place
Using:
@foo = (2, 3, 6, 7);
reject { $_ > 3 } @foo;
@foo = (2, 3, 6, 7);
$n = reject { $_ > 3 } @foo;
@foo = (2, 3, 6, 7);
@cpy = reject { $_ > 3 } @foo;
Implementation:
sub reject(&\@) {
my ($block, $ary) = @_;
return grep {! $block->() } @$ary if wantarray;
my $i = 0;
for (@$ary) {
next if $block->();
($ary->[$i], $_) = ($_, $ary->[$i]);
$i++;
}
$#$ary = $i - 1;
return scalar(@$ary) if defined(wantarray);
}
source
share