Find in assoc array then iterate

Tejas notrealemail at gmail.com
Sat Oct 22 15:12:42 UTC 2022


On Friday, 21 October 2022 at 22:03:53 UTC, Kevin Bailey wrote:
> I'm trying to do this equivalent C++:
>
>     unordered_map<string, int> map;
>
>     for (auto i = map.find(something); i != map.end(); ++i)
>         ...do something with i...
>
> in D, but obviously with an associative array. It seems that 
> it's quite
> easy to iterate through the whole "map", but not start from the 
> middle.
> Am I missing something obvious (or not so obvious) ?

Maybe the following can help?

```d
import std;

void main(){
     int[int] a = [2 : 4,
                   5 : 6,
                   6 : 7,
                   10 : 12,
                   11 : 23
                   ];
     bool cont = true;
     foreach(k, v; a){ // use ref v instead of v if you wanna 
modify the values, like siarhei did in his post
         if (v != 7 && cont){
             continue;
         }
         else{
             cont = false;
         	writeln(k, ":", v);
         }
     }
     /+
     foreach(k, v; a.skipOver!((a, b) => a != b)(6)){ // this code 
could've worked if you had an inputRange, I think
         writeln(k, ":", v);
     }
     +/
}

```


More information about the Digitalmars-d-learn mailing list