[tpm] how to delete array entries en-passant

Shlomi Fish shlomif at iglu.org.il
Wed Jun 30 09:02:55 PDT 2010


On Wednesday 30 Jun 2010 18:49:10 Indy Singh wrote:
> Modifying the array while iterating through the array is usually something
> to be avoided when writing clear code.  Don't do it.

Well, you are allowed to modify the elements themselves one at a time, using 
something like:

foreach my $var (@array)
{
	$var = SOMETHING_ELSE($var);
}

But modifying the order of the elements should be avoided.

> 
> What would you expect the foreach function to return when you are modifying
> the list under its feet?

A few notes - in Perl, foreach is not a function - it's a loop construct with 
some magic. And one should not rely on the return value of a foreach list - 
use it only for side-effects.

> 
> You may also want to look at using the grep and map functions.
> 

Right.

Regards,

	Shlomi Fish

> Indy Singh
> IndigoSTAR Software -- www.indigostar.com
> 

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Why I Love Perl - http://shlom.in/joy-of-perl

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