suggested change to foreach index

BCS BCS_member at pathlink.com
Wed Jun 7 19:14:37 PDT 2006


In article <e67qth$ugk$1 at digitaldaemon.com>, Ameer Armaly says...
>
>
>"BCS" <BCS at pathlink.com> wrote in message 
>news:e67p90$rol$1 at digitaldaemon.com...
>>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??
>Wouldn't a function return serve a similar if not identical purpose? 
>
>
Yes, but that would require putting the foreach in a function. If it needs to
access local variables then it needs to be a nested function and therefore could
results in a lot of lookup by pointer. Also there is the added function call
overhead associated with it. If the code is performance critical all of these
are bad things.





More information about the Digitalmars-d mailing list