> I always just use this, because I hate removing elements and worrying about 
> it:
> 
> use strict;
> use warnings;
> sub remove_el (\@@) {
>     my $array = shift;
>     my @el_rem;
>     for (sort {$b <=> $a} @_) {
>         if (! exists $array->[$_]) {
>             warn 'element ', $_, ' does not exist';
>             next;
>         }
>         push @el_rem, splice(@$array, $_, 1);
>     }
>     return @el_rem;
> };
> 
> my @sample = ('a', 'b', 'c', 'd', 'e', 'f', 'g');
> remove_el @sample, 6, 2, 4, 1;
> 
> If you just need to remove 1 element a time or a length of elements, you 
> should just use splice. But sometimes when you have to remove different
> elements 
> in no particular order at no particular time, it gets annoying. Especially if
> 
> you are doing something and you have to remove element 3 and 8 at the same 
> time, and if you remove 3 first, element 8 will be different. We have
> subroutines 
> to make considering this stuff more than once minimal. 
> 

I had to stare at that for a while before I realised you were sorting the
splice points in reverse order to ensure that the removal of
one didn't change the indexes of all the others.

Very Nice Code. (tm)

Now if only I could get the index of an array element without
grepping it!



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to