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/;
my @array;
for my $index (reverse sort @indices) {
splice @array, $index, 0;
}
12.04.17 22:19, Uri Guttman пишет:
On 04/12/2017 03:00 PM, David Emanuel da Costa Santiago wrote:
Hello!
What's the best way to delete multiple indices from an array?
i'm doing:
---------------
my @array=qw/zero one two three four five six seven eight nine ten/;
my @indicesToDelete = (2,4,6,7);
if you have the indexes to keep, this would be a simple slice:
my @keep_indexes = ( 0, 1, 3, 5, 8,9 10 );
@array = @array{ @keep_indexes } ;
so one idea is to make that list of kept indexes from the list of
indexes to delete:
my %keep_hash ;
@keep_hash{ 0 .. 10 } = () ; # no need for any values so this save space
delete @keep_hash{ @keep_indexes } ;
@array = @array{ keys %keep_hash } ;
my %hash;
@hash{(0..scalar(@array)-1)} = @array;
delete $hash{$_} for @indicesToDelete;
delete can be used on a slice
delete @hash{ @indicesToDelete } ;
uri
Perl Guru for hire. Available for contract/full time/part time Perl
hacking.
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/