suggested change to foreach index

Derek Parnell derek at psych.ward
Wed Jun 7 18:14:19 PDT 2006


On Wed, 07 Jun 2006 17:04:09 -0700, BCS wrote:

> 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??

I see what you mean but have you tried this ...

    int i;
    char[] arr;
    foreach(int j, c; arr)
        if(' ' == c)
        {
            arr = arr.dup;
            foreach(inout c; arr[j..$])
               if(' ' == c) c = '_';
            break;
        }

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
8/06/2006 11:12:32 AM



More information about the Digitalmars-d mailing list