Thanks for your reply.
First of all, my example was not about performance, but was about an
idea. So you are correct about qw and sort. I'm sorry if someone saw
this example and decide to use it without reading perldoc and
understanding what we do.
We need to reverse sort because when we start to splice elements from
array indices will be change. If we delete first element, third element
became second, fourth became third and so on.
12.04.17 23:19, Uri Guttman пишет:
On 04/12/2017 04:08 PM, Илья Рассадин wrote:
Hi!
You can use splice to delete elements from array.
To delete multiple elements you need to do splice in a loop
my @indices = qw/2 4 5 7/;
why are you using qw which makes strings and not a list of integers?
my @array;
for my $index (reverse sort @indices) {
sort defaults to a lexical sort which won't work well on integers with
more than 2 digits.
and why are you sorting and reversing the indexes? i don't think that
will help make the splice more efficient if any of the indexes are not
at the end of the array.
splice @array, $index, 0;
you need a length of 1 to remove the element. that line is a no-op.
uri
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/