merging container arrays
Vino
akashvino79 at gmail.com
Sat Oct 31 15:16:22 UTC 2020
Hi All,
Request your help on the below code, the requirement is that
result's are stored in one single container.
Code:
import asdf;
import std.algorithm: map;
import std.container.array;
import std.stdio: writeln;
import std.typecons: Tuple, tuple;
import std.range: lockstep;
auto api1()
{
string apidata1 = `{"items": [
{"name":"T01","hostname":"test01","pool":"Development"},
{"name":"T02","hostname":"test02","pool":"Quality"},
{"name":"T03","hostname":"test03","pool":"Production"}
]}`;
Array!(Tuple!(string, string, string)) data1 =
parseJson(apidata1)["items"].byElement
.map!(item => tuple(
item["name"].get!string("default"),
item["hostname"].get!string("default"),
item["pool"].get!string("default")
));
return data1[];
}
auto api2()
{
string apidata2 = `{"items": [
{"hostname":"test01","type":"Development"},
{"hostname":"test02","type":"Quality"},
{"hostname":"test03","type":"Production"}
]}`;
Array!(Tuple!(string, string)) data2 =
parseJson(apidata2)["items"].byElement
.map!(item => tuple(
item["hostname"].get!string("default"),
item["type"].get!string("default")
));
return data2[];
}
auto api3()
{
string apidata3 = `{"items": [
{"type":"Development","location":"L1"},
{"type":"Quality","location":"L2"},
{"type":"Production","location":"L3"}
]}`;
Array!(Tuple!(string, string)) data3 =
parseJson(apidata3)["items"].byElement
.map!(item => tuple(
item["type"].get!string("default"),
item["location"].get!string("default")
));
return data3[];
}
void main()
{
auto apidata1 = api1;
auto apidata2 = api2;
auto apidata3 = api3;
if(!apidata1.empty) {
foreach(ref x , y; lockstep(apidata1[], apidata2[])) {
if(x[1] == y[0]){
writeln(y[1]); // the output needs to be merged with
apidata1
}
}
}
if(!apidata2.empty) {
foreach(ref x, y; lockstep(apidata2[], apidata3[])) {
if(x[1] == y[0]) {
writeln(y[1]); // the output needs to be merged with
apidata1
}
}
}
writeln(apidata1[]); // Should
contain(name,hostname,pool,type,location)
}
From,
Vino.B
More information about the Digitalmars-d-learn
mailing list