How to dynamically calculate an index?

anonymouse anony at mouse.com
Tue Jul 12 15:25:45 UTC 2022


On Tuesday, 12 July 2022 at 14:55:47 UTC, anonymouse wrote:
>
> I've tried using a foreach loop to achieve this but failed 
> miserably.
>
> --anonymouse

Wait, I think I've got it.

This little snippet does the trick:

     int index;
     foreach(i, v; a) {
         int t = v;
         foreach(d; dims[i+1 .. a.length])
             tmp *= d;
         index += tmp;
     }

Thanks, everyone.
--anonymouse


More information about the Digitalmars-d-learn mailing list