I use the following code to exclude elements in @{$x}
at indexes in @{$index}
. But I am not sure it is the most efficient way to implement this function. Does anybody have any better way to do.
sub arrayexclude {
my $x = shift;
my $index = shift;
my @keep_index = (0 .. (scalar(@{$x})-1));
delete @keep_index[@{$index}];
my $result=[];
for my $i (@keep_index) {
if(defined $i) {
push @{$result}, @{$x}[$i];
}
}
return $result;
}
You don't need anything beyond an array slice, so I'd avoid creating a sub just for this.
my @wanted = @array[@indices];
If you're working with array references, the same recipe applies:
my @wanted = @{$array}[@$indices];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With