What is the “correct” way to remove array elements in Perl?

I have an array containing a set of elements. The order of the elements does not matter - I use an array, as this is the simplest data structure that I know in Perl.

my @arr = ...
while (some condition) {
    # iterate over @arr and remove all elements which meet some criteria
    # (which depends on $i)
}

I know splice(), but I think it’s not good using it during iteration. deletefor array elements seems deprecated. Perhaps use grepon @arrin ( @arr = grep {...} @arr)?

What is the best practice here?

Perhaps use a hash (although I really don't need this)?

+5
source share
2 answers

Your idea of ​​using grep is good

@arr = grep { cond($i++); } @arr;
+7
source

delete , , Perl.

:

@arr = @arr[ @indices ];

perldata.

+7

All Articles