[Israel.pm] How to remove elements according to a filter from an array

yaron at kahanovitch.com yaron at kahanovitch.com
Fri May 25 00:36:44 EEST 2007


Hi,

Working with delete makes the value at the given index be undefined, so going backward or forward is irrelevant.
If you wish to take out the element completely you need to use splice. In that case you will have to make sure not to miss elements.

4 example (Untested)

my $index = 0;
while ($index < scalar(@ARRAY)) {
 if (criteria) { splice (@ARRAY,$index,1);}
 else { ++$index;}
}

Hope that clear up things
Yaron Kahanovitch

----- Original Message -----
From: "Peter Gordon" <peter at pg-consultants.com>
To: "Perl in Israel" <perl at perl.org.il>
Sent: 22:18:48 (GMT+0200) Africa/Harare יום חמישי 24 מאי 2007
Subject: Re: [Israel.pm] How to remove elements according to a filter from	an array

It is better to iterate backwards over the array.

This method is going to miss elements.

On Thu, 2007-05-24 at 15:54 -0400, yaron at kahanovitch.com wrote:
> Hi
> 
> See perldoc -f delete.
> 
> 4 example (Untested)
> 
> foreach $index (0 .. $#ARRAY) {
>         delete $ARRAY[$index] if (criteria);
>     }
> 
> 
> YAron Kahanovitch
> ----- Original Message -----
> From: "Yossi Itzkovich" <Yossi.Itzkovich at ecitele.com>
> To: "Perl in Israel" <perl at perl.org.il>
> Sent: 20:36:29 (GMT+0200) Africa/Harare יום חמישי 24 מאי 2007
> Subject: [Israel.pm] How to remove elements according to a filter from an array
> 
> Hi,
> 
> Assuming I have a big array, and I want to remove elemnts according to a
> criteria, how do I do it ?
> Please note, I am not asking about getting a new list of elements that
> passed the filter (like grep does), but removing the items from the
> original list.  For a big array (and assuming that arrays are
> implemented with linked list (is it ?)), then removing items from the
> original list might be much faster.
> 
> 
> Yossi
> 
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
> 
> 
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl


_______________________________________________
Perl mailing list
Perl at perl.org.il
http://perl.org.il/mailman/listinfo/perl





More information about the Perl mailing list