I have an array as below :
my @arr = qw (ram sam ham dam yam fam lam cam)
I want to remove sam
, yam
and lam
from the above array using splice
.
I am using below code to do it :
# first getting the index of sam
(my $ind ) = grep {$arr[$_] eq 'sam'} 0 .. $#arr;
splice(@arr, $ind, 1);
My question is since the array elements have changed their position, I need to again fetch the index of yam
and lam
and do a splice on the array.
Is there any other way so that in one-go I will remove the random elements from the array using splice. I can get the index of all of them in once and delete them from the array but those positions will hold undef value which I do not want.
splice
? Any particular reasons for that?my %h = map {$_=>1} qw(sam yam lam); @arr = grep { !defined $h{$_} } @arr;
?