www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Creating Struct for an output of a program.

reply Vino <vino.bheeman hotmail.com> writes:
Hi All,

  Request your help on how to create a struct with the output of 
the below program.

Program:
import std.algorithm: all, map, filter;
import std.stdio: File, writeln;
import std.typecons: Tuple, tuple;
import std.container.array;
import std.string: split, strip;
import std.uni: isWhite, toLower;
import std.range: chunks;

void main () {
Array!string TableData, StructureData;
auto Meta = 
File("C:\\Users\\bheev1\\Desktop\\Current\\Script\\Others\\meta\\meta.txt",
"r");
auto MetaData = Array!(Tuple!(string, string))(Meta.byLineCopy()
.filter!(line => !line.all!isWhite)
.map!(a => a.split(":"))
.map!(a => tuple(a[0].toLower.strip, a[1].toLower.strip)));
foreach (line; MetaData[]) { TableData.insertBack(line[0]); 
StructureData.insertBack(line[1]); }
for(int i = 0; i < TableData[].length; i++ ) {
auto S1 = StructureData[i].split(",").chunks(3);
auto S2 = S1.map!(a => tuple(a[0],a[1],a[2]));
for(int z =0; z < S2.length; z++)
{ writefln("%-8s %;s", S2[z][1] , S2[z][0]); }
}
}

Output:
string name;
string country;
int age;


Need to create as struct using the output

struct Layout {
{
string name;
string country;
int age;

}

From,
Vino.B
Jan 08 2018
parent reply Mengu <mengukagan gmail.com> writes:
On Tuesday, 9 January 2018 at 07:57:19 UTC, Vino wrote:
 Hi All,

  Request your help on how to create a struct with the output of 
 the below program.

 Program:
 import std.algorithm: all, map, filter;
 import std.stdio: File, writeln;
 import std.typecons: Tuple, tuple;
 import std.container.array;
 import std.string: split, strip;
 import std.uni: isWhite, toLower;
 import std.range: chunks;

 void main () {
 Array!string TableData, StructureData;
 auto Meta = 
 File("C:\\Users\\bheev1\\Desktop\\Current\\Script\\Others\\meta\\meta.txt",
"r");
 auto MetaData = Array!(Tuple!(string, string))(Meta.byLineCopy()
 .filter!(line => !line.all!isWhite)
 .map!(a => a.split(":"))
 .map!(a => tuple(a[0].toLower.strip, a[1].toLower.strip)));
 foreach (line; MetaData[]) { TableData.insertBack(line[0]); 
 StructureData.insertBack(line[1]); }
 for(int i = 0; i < TableData[].length; i++ ) {
 auto S1 = StructureData[i].split(",").chunks(3);
 auto S2 = S1.map!(a => tuple(a[0],a[1],a[2]));
 for(int z =0; z < S2.length; z++)
 { writefln("%-8s %;s", S2[z][1] , S2[z][0]); }
 }
 }

 Output:
 string name;
 string country;
 int age;


 Need to create as struct using the output

 struct Layout {
 {
 string name;
 string country;
 int age;

 }

 From,
 Vino.B
if S2 consists of data for Layout struct, then you can simply do: auto S2 = S1.map!(a => Layout(a[0], a[1], a[2])); which will give you a range of Layout.
Jan 09 2018
parent Vino <vino.bheeman hotmail.com> writes:
On Tuesday, 9 January 2018 at 12:50:04 UTC, Mengu wrote:
 On Tuesday, 9 January 2018 at 07:57:19 UTC, Vino wrote:
 [...]
if S2 consists of data for Layout struct, then you can simply do: auto S2 = S1.map!(a => Layout(a[0], a[1], a[2])); which will give you a range of Layout.
Hi, We want the Layout struct to be created from the output of S1, in the above the Layout is a example of the struct structure that we needed. From, Vino.B
Jan 09 2018