in reply to Remove array elements dynamically!
However, it's much easier and more efficient to create a new array instead of splicing the same array, since each splice needs to copy all the elements from $i to the end of the array rearrange/copy the pointers to the elements from element $i to the end of the array.
Depending on your algorithm, you could probably do that at the same time/instead of building the @arrayindex array.my @newarray; foreach my $i (@arrayindex) { push @newarray,$array[$i]; }
Another way would be to use grep (if you can determine which elements to keep based solely on their value):
or evenmy @kept = grep { $_ < 4 and $_ > 10 } @array;
@array = grep { $_ < 4 and $_ > 10 } @array;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Remove array elements dynamically!
by shmem (Chancellor) on Aug 27, 2007 at 19:59 UTC | |
by Joost (Canon) on Aug 27, 2007 at 20:11 UTC | |
|
Re^2: Remove array elements dynamically!
by suaveant (Parson) on Aug 28, 2007 at 20:21 UTC | |
by Joost (Canon) on Aug 28, 2007 at 20:23 UTC | |
|
Re^2: Remove array elements dynamically!
by newbio (Beadle) on Aug 27, 2007 at 20:05 UTC |