[tpm] how to delete array entries en-passant

Shlomi Fish shlomif at iglu.org.il
Wed Jun 30 08:41:06 PDT 2010


Hi Fulko,

On Wednesday 30 Jun 2010 18:06:49 Fulko Hew wrote:
> As the subject line asks...
> what I'd like to do is something like
> 
> foreach (@array) {
>     if (condition) {
>         splice @array, this_entry, 1;
>         next;
>     }
>     do processing on this entry;
> }
> 
> @array now contains a sub-set of the original
> that I can now further process
> 

It's always a bad idea to modify the order of an array's elements while 
iterating over it using foreach. Don't do that.

I should also note splicing an index out of the middle of an array is an O(N) 
operation where N is the number of elements in the array.

> 
> 
> 
> 'delete' might work, but the words say delete on arrays is deprecated
> and it still doesn't give me the delete 'this' entry concept, because
> it too works on an index.
> 
> I'd like to do it without creating another array, because I like
> 'premature optimizations' and because I think it would make the
> code more readable.  :-)

It will likely make things slower and less readable (assuming it will work at 
all), so don't do that.

Regards,

	Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Freecell Solver - http://fc-solve.berlios.de/

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .


More information about the toronto-pm mailing list