suggested change to foreach index

Ameer Armaly ameer_armaly at hotmail.com
Wed Jun 7 17:27:51 PDT 2006


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





More information about the Digitalmars-d mailing list