| List-Extract documentation | Contained in the List-Extract distribution. |
List::Extract - grep and splice combined
use List::Extract 'extract';
my @keywords = qw/
foo
!bar
baz
/;
my @exclude = extract { s/^!// } @keywords;
print "Keywords: @keywords\n";
print "Exclude: @exclude\n";
__END__
Keywords: foo baz
Exclude: bar
List::Extract exports a grep-like routine called extract that both returns and extracts the elements that tests true. It's grep and splice combined.
Nothing is exported by default. The :ALL tag exports everything that can be exported.
Removes the elements from array for which BLOCK returns true. In list context the elements are returned in original order. In scalar context the number of removed elements is returned.
In BLOCK the elements in ARRAY will be accessible through $_. Modifications to $_ will be preserved in the returned list, but discarded for elements left in the array.
Johan Lodin <lodin@cpan.org>
Copyright 2007-2008 Johan Lodin. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| List-Extract documentation | Contained in the List-Extract distribution. |
package List::Extract; use 5.006001; $VERSION = 0.03; use Exporter; @ISA = Exporter::; @EXPORT_OK = qw/ extract /; $EXPORT_TAGS{ALL} = \@EXPORT_OK; use strict; sub extract (&\@) { my ($code, $array) = @_; my (@keep, @extracted); for my $orig (@$array) { local $_ = $orig; if ($code->()) { push @extracted, $_; } else { push @keep, $orig; } } @$array = @keep; return @extracted; } 1; __END__