Thanks for your reply. I didn't think about that! :-)

While i was reading your email, i remembered about splice, so i guess
i'm going to end up with:

----------
my @array=qw/zero one two three four five six seven eight nine ten/;

my @indicesToDelete = (2,4,6,7);

my $deviation = 0;
splice(@array, $_-$deviation++,1) for(@indicesToDelete);
----------

Thank you all!


Regards,
David Santiago


On Wed, 12 Apr 2017 15:19:33 -0400
Uri Guttman <u...@stemsystems.com> wrote:

> 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/


Reply via email to