canFind all elements in a array.

sarn sarn at theartofmachinery.com
Tue Nov 10 09:47:06 UTC 2020


On Tuesday, 10 November 2020 at 08:19:15 UTC, Vino wrote:
> foreach(i; data2[]) {
>    if(data1[].canFind(i[0])) {
>      writeln(i[1]);
>   }
> }

This is iterating over all the elements in data2 and outputting 
some of them, so the output will never be longer than data2.

It looks like you want to iterate over data1.  Something like 
this:

foreach(i; data1[]) {
   auto result = data2[].find!((p, x) => p[0] == x)(i);
   if (!result.empty) writeln(result.front[1]);
}

However, you could also use an associative array for data2:

string[string] data2 = [
   "DEV Systems": "DEV Cluster",
   "QAS Systems": "QAS Cluster",
];

foreach (i; data1[]) {
   if (auto v = i in data2) writeln(*v);
}

The "in" operator returns a pointer to the value in data2 at 
index "i", or else a null pointer.

See more info here:
https://ddili.org/ders/d.en/aa.html
https://dlang.org/spec/hash-map.html



More information about the Digitalmars-d-learn mailing list