deleting items from 2d arrays

Sergey Gromov snake.scaly at gmail.com
Fri Aug 14 10:11:42 PDT 2009


Thu, 13 Aug 2009 22:59:37 -0400, Michael P. wrote:

> foreach( Block[] ba; level )
> {
> 	foreach( Block b; ba )
> 	{
> 		if( checkCollision( ball, b.p ) )
> 		{
> 			//remove the block??
> 		}
> 	}
> }

I like Daniel's answer better.  But if order matters you could do this:

for( int i = 0; i < level.length; )
{
	for( int j = 0; j < level[ i ].length; )
	{
		if( checkCollision( ball, level[ i ][ j ].p ) )
		{
			level[ i ] = level[ i ][ 0 .. j ] ~ level[ i ][ j+1 .. $ ];
		}
		else
		{
			j++;
		}
	}
	if( level[ i ].length == 0 )
	{
		level = level[ 0 .. i ] ~ level[ i+1 .. $ ];
	}
	else
	{
		i++;
	}
}


More information about the Digitalmars-d-learn mailing list