suggested change to foreach index

BCS BCS at pathlink.com
Wed Jun 7 17:04:09 PDT 2006


I often find my self doing something like this:

<code>
int i
char[] arr;

foreach(int j, c; arr)
	if(' ' == c)
	{
		i=j;	// copy j out of foreach's scope
		break;
	}

if(i < arr.length)
{
	arr = arr.dup;
		// use copy of j
	foreach(inout c; arr[i..$])
		if(' ' == c) c = '_';
}
</code>

In this case it would be nice to be able to use a variable in the outer 
scope as the index in the foreach, e.i.

<code>
int i
char[] arr;

foreach(int i, c; arr)
	if(' ' == c)
		break;

// i keeps the value from the last time through
...
</code>


Thoughts comments??



More information about the Digitalmars-d mailing list