How to delete multiple indices from array

2017-04-12 Thread David Emanuel da Costa Santiago
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); my %hash; @hash{(0..scalar(@array)-1)} = @array; delete $hash{$_} for @indicesToDelete; @arr

Re: How to delete multiple indices from array

2017-04-12 Thread 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 kee

Re: How to delete multiple indices from array

2017-04-12 Thread David Emanuel da Costa Santiago
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(@arra

Re: How to delete multiple indices from array

2017-04-12 Thread Uri Guttman
On 04/12/2017 04:01 PM, David Emanuel da Costa Santiago wrote: 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/; m

Re: How to delete multiple indices from array

2017-04-12 Thread Илья Рассадин
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

Re: How to delete multiple indices from array

2017-04-12 Thread 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

Re: How to delete multiple indices from array

2017-04-12 Thread Shlomi Fish
Hi Uri! Some notes. On Wed, 12 Apr 2017 15:19:33 -0400 Uri Guttman 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 th

Re: How to delete multiple indices from array

2017-04-12 Thread Uri Guttman
On 04/12/2017 04:38 PM, Shlomi Fish wrote: Hi Uri! Some notes. On Wed, 12 Apr 2017 15:19:33 -0400 Uri Guttman 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

Re: How to delete multiple indices from array

2017-04-12 Thread David Emanuel da Costa Santiago
Thank you all for your replies. I tested the 5 solutions with the benchmark module, and the splice one is the fastest: Benchmark: timing 99 iterations of each, grep, hash, keep, splice... each: 5 wallclock secs ( 5.07 usr + 0.00 sys = 5.07 CPU) @ 197238.46/s (n=99) grep: 5 wallc

Re: How to delete multiple indices from array

2017-04-12 Thread Paul Johnson
On Wed, Apr 12, 2017 at 11:12:45PM +0200, David Emanuel da Costa Santiago wrote: > Thank you all for your replies. > > I tested the 5 solutions with the benchmark module, and the splice one > is the fastest: > The hash one, which was my first solution is the slowest one. :-( But they're all fas

Re: How to delete multiple indices from array

2017-04-12 Thread Shawn H Corey
On Wed, 12 Apr 2017 16:19:32 -0400 Uri Guttman wrote: > 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 w

Re: How to delete multiple indices from array

2017-04-12 Thread X Dungeness
Quite! Even the slowest may find redemption in clarity :) my @arr = qw( zero one two three ... ); my @del = qw( 2,4,... ); my( %del, @new ); @del{ @del } = (); for (0..$#arr) { push @new, $arr[$_] unless exists $del{$_}} On Wed, Apr 12, 2017 at 2:18 PM, Paul Johnson wrote: > On Wed, Apr 12, 20

Re: How to delete multiple indices from array

2017-04-12 Thread Uri Guttman
On 04/12/2017 05:42 PM, Shawn H Corey wrote: On Wed, 12 Apr 2017 16:19:32 -0400 Uri Guttman wrote: 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?

Re: How to delete multiple indices from array

2017-04-12 Thread Shawn H Corey
On Wed, 12 Apr 2017 18:16:56 -0400 Uri Guttman wrote: > On 04/12/2017 05:42 PM, Shawn H Corey wrote: > > On Wed, 12 Apr 2017 16:19:32 -0400 > > Uri Guttman wrote: > > > > > > my @array; > > > > for my $index (reverse sort @indices) { > >> sort defaults to a lexical sort which won't work well on

Re: How to delete multiple indices from array

2017-04-12 Thread Uri Guttman
On 04/12/2017 07:29 PM, Shawn H Corey wrote: On Wed, 12 Apr 2017 18:16:56 -0400 Uri Guttman wrote: It's not about saving time. It's about removing a bug. In the code below, notice that there are two different outputs depending on the order that splice is applied. It's only by using descending i

Re: How to delete multiple indices from array

2017-04-12 Thread Shawn H Corey
On Wed, 12 Apr 2017 19:34:22 -0400 Uri Guttman wrote: > it is so odd to be filtering an array by the indexes. i smell an XY > problem here. i wonder what the real goal is vs how to solve it this > way. > > thanx, > > uri I was thinking that too. -- Don't stop where the ink does. S

Re: How to delete multiple indices from array

2017-04-12 Thread Илья Рассадин
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 st