What is the best way to exclude elements from an array by indexes in Perl?

Viewed 605

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;
}
3 Answers
Related