Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the 'best' way to delete multiple non-sequential elements in a Perl array?

Tags:

arrays

perl

While executing a script, I need to delete multiple elements (these elements are not sequential) of an array. I will get my array and indexes while executing the script.

For example:

I may get an array and list of indexes like below:

my @array = qw(one two three four five six seven eight nine);

my @indexes = ( 2, 5, 7 );

I have below subroutine to do this:

sub splicen {
    my $count     = 0;
    my $array_ref = shift @_;

    croak "Not an ARRAY ref $array_ref in $0 \n"
        if ref $array_ref ne 'ARRAY';

    for (@_) {
        my $index = $_ - $count;
        splice @{$array_ref}, $index, 1;
        $count++;
    }

    return $array_ref;
}

If I call my subroutine like below:

splicen(\@array , @indexes);

That works for me but:

Is there any better way to do this?

like image 390
Malli Paritala Avatar asked Jan 11 '23 00:01

Malli Paritala


1 Answers

If instead you splice from the end of the array, you won't have to maintain the offset $count:

sub delete_elements {
    my ( $array_ref, @indices ) = @_;

    # Remove indexes from end of the array first
    for ( sort { $b <=> $a } @indices ) {
        splice @$array_ref, $_, 1;
    }
}
like image 52
LeoNerd Avatar answered May 26 '23 02:05

LeoNerd