www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - merging container arrays

reply Vino <akashvino79 gmail.com> writes:
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
Oct 31 2020
parent reply Vino <akashvino79 gmail.com> writes:
On Saturday, 31 October 2020 at 15:16:22 UTC, Vino wrote:
 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
Hi All, I was able to find a solution using a different approach, the code is in the link https://run.dlang.io/is/Jx4NLw, request your help on to get the value using the Key In PHP we can get the value of the associative array using the key as below PHP: foreach($array as $k) { print_r($k["Name"]); } So request you help on how the same in d Tired the below , no luck 1 > foreach(i; data.byKey) { writeln(i["Name"]); } Error: onlineapp.d(53,37): Error: cannot implicitly convert expression `"Name"` of type `string` to `ulong` 2 > foreach (ref i; data) writeln(i["Name"]); 3 > foreach(i; data.byValue) { writeln(i["Name"]); } 4 > foreach(i; data) { writeln(get(i["Name"])); } Error: onlineapp.d(53,38): Error: function `std.container.array.Array!string.Array.opIndex(ulong i) inout` is not callable using argument types `(string)` onlineapp.d(53,38): cannot pass argument `"Name"` of type `string` to parameter `ulong i` From, Vino.B
Oct 31 2020
parent Andre Pany <andre s-e-a-p.de> writes:
On Sunday, 1 November 2020 at 05:23:42 UTC, Vino wrote:
 On Saturday, 31 October 2020 at 15:16:22 UTC, Vino wrote:
 [...]
Hi All, I was able to find a solution using a different approach, the code is in the link https://run.dlang.io/is/Jx4NLw, request your help on to get the value using the Key [...]
I assume it should be foreach(i; data.byKey) { writeln(data[i]); } Kind regards Andre
Nov 01 2020