suggested change to foreach index

BCS BCS_member at pathlink.com
Wed Jun 7 19:10:58 PDT 2006


In article <cs4o03emrdkm$.b5qasiolumf7.dlg at 40tude.net>, Derek Parnell says...
>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 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;
>        }
>

That would work but its not vary expandable. Try doing that for five or six
loops. The proposed solution is just a lot cleaner, even in the two loop case.

Or how about doing that with something like:

char[] arr;
int j=0, i, skip;

do
{
foreach(inout i, c; arr[j..$])
if(‘ ' == c)
break;

// do something and continue
j+=(i+skip);
}while(i != arr.length)





More information about the Digitalmars-d mailing list