digitalmars.D.learn - canFind all elements in a array.
Hi All,
Request your help, the below code output's as below hence
request your help on hot to get the output as below(Required
Output).
Output
DEV Cluster
QAS Cluster
Required Output
DEV Cluster
DEV Cluster
DEV Cluster
QAS Cluster
Code
import std.container.array;
import std.stdio: writeln;
import std.algorithm: canFind;
import std.typecons: Tuple, tuple;
void main () {
Array!string data1;
Array!(Tuple!(string,string)) data2;
Array!string rs;
data1.insertBack("DEV Systems");
data1.insertBack("DEV Systems");
data1.insertBack("DEV Systems");
data1.insertBack("QAS Systems");
data2.insertBack(tuple("DEV Systems","DEV Cluster"));
data2.insertBack(tuple("QAS Systems","QAS Cluster"));
foreach(i; data2[]) {
if(data1[].canFind(i[0])) {
writeln(i[1]);
}
}
}
From,
Vino.B
Nov 10 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
Nov 10 2020
On Tuesday, 10 November 2020 at 09:47:06 UTC, sarn wrote:On Tuesday, 10 November 2020 at 08:19:15 UTC, Vino wrote:Hi Sarn, Thank you very much[...]This is iterating over all the elements in data2 and outputting some of them, so the output will never be longer than data2. [...]
Nov 10 2020








Vino <akashvino79 gmail.com>