Use grep if you know the elements you want to keep.
my @keepers = grep /interesting/, @array;
If you have an array reference instead, write
my @keepers = grep /interesting/, @$arrayref;
Note that this does not modify the array.
The process is similar to map . Given a test that rejects items, write
my @keepers = map /do not want/ ? () : $_, @array;
Invert the meaning of the test with
my @keepers = map /interesting/ ? $_ : (), @array;
Greg bacon
source share