Some quick questions
Regan Heath
regan at netmail.co.nz
Wed Aug 8 01:13:16 PDT 2007
BCS wrote:
> Reply to Márcio,
>
>> Hi, I have some quick questions:
>>
>> (1.) Why does version 2.0 of the compiler doesn't allow changing the
>> index of a foreach loop? For example:
>>
>> ;string format = "%s";
>> ;
>> ;foreach (i, c; format) {
>> ; if (c != '%') {
>> ; // ...
>> ; continue;
>> ; }
>> ;
>> ; switch (format[++i]) {
>> ; case 's':
>> ; // ...
>> ; break;
>> ; // ...
>> ; }
>> ;}
>
> every time I see the index changed my toes curl. It just seems wrong.
>
> I'd do this:
>
> ;string format = "%s";
> ;int next = 0;
> ;foreach (i, c; format)
> ;{
> ; if(i != next) continue;
> ; if (c != '%')
> ; {
> ; // ...
> ; continue;
> ; }
> ; next = i + 1;
> ; switch (format[next])
> ; {
> ; case 's':
> ; // ...
> ; break;
> ; // ...
> ; }
> ;}
I'd use a for loop. That said it would be nice if there was a nicer way
to do this with foreach as it's the only 'container agnostic' way of
iterating, right?
Regan
More information about the Digitalmars-d-learn
mailing list