Thread overview |
---|
January 09, 2018 Storing struct in a array | ||||
---|---|---|---|---|
| ||||
Hi All, It is possible to store struct in a array ans use the same in csvReader e.g. import std.stdio; import std.container.array; void main () { Array!string a; struct Layout { string name; int value; double other; } a.insert(Layout); auto record = uFile.byLineCopy().joiner("\n").csvReader!(Tuple!a[0])) foreach (record; records) { writeln(record.name); writeln(record.value); writeln(record.other); } From, Vino.B |
January 09, 2018 Re: Storing struct in a array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vino | On Tuesday, 9 January 2018 at 13:49:41 UTC, Vino wrote: > Hi All, > > It is possible to store struct in a array ans use the same in csvReader e.g. > > import std.stdio; > import std.container.array; > > void main () { > Array!string a; > struct Layout { string name; int value; double other; } > > a.insert(Layout); > auto record = uFile.byLineCopy().joiner("\n").csvReader!(Tuple!a[0])) > foreach (record; records) > { > writeln(record.name); > writeln(record.value); > writeln(record.other); > } > > From, > Vino.B Hi All, Was able to find on hot to store a struct in an array, but not able to use that array in csvReader Program: import std.algorithm: joiner; import std.container.array; import std.csv: csvReader; import std.stdio: File, writeln; import std.typecons: Tuple, tuple; void main () { auto fName = "C:\\Users\\bheev1\\Desktop\\Current\\Script\\Others\\Table1.csv"; auto uFile = File(fName, "r"); struct T1 { string Name; string Country; int Age; } Array!T1 T1s; foreach (record; uFile.byLineCopy().joiner("\n").csvReader!(Tuple!T1s)) writeln(record); } Error: C:\D\dmd2\windows\bin\..\..\src\phobos\std\typecons.d(523): Error: template instance parseSpecs!(T1s) cannot use local 'T1s' as parameter to non-global template parseSpecs(Specs...) C:\D\dmd2\windows\bin\..\..\src\phobos\std\typecons.d(635): Error: CTFE failed because of previous errors in injectNamedFields ArrayStruct.d(12): Error: template instance ArrayStruct.main.Tuple!(T1s) error instantiating Failed: ["dmd", "-v", "-o-", "ArrayStruct.d", "-I."] From, Vino.B |
January 09, 2018 Re: Storing struct in a array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vino | On Tuesday, 9 January 2018 at 13:49:41 UTC, Vino wrote: > Hi All, > > It is possible to store struct in a array ans use the same in csvReader Sure, you can just pass the type of your struct to csvReader: struct Layout { string name; int value; double other; } auto readArrayOfStructs(string fname) { Array!Layout res; foreach(record; fname.readText.csvReader!Layout('\t')) { res ~= record; } return res; } |
January 09, 2018 Re: Storing struct in a array | ||||
---|---|---|---|---|
| ||||
Posted in reply to thedeemon | On Tuesday, 9 January 2018 at 17:00:05 UTC, thedeemon wrote: > On Tuesday, 9 January 2018 at 13:49:41 UTC, Vino wrote: >> Hi All, >> >> It is possible to store struct in a array ans use the same in csvReader > > Sure, you can just pass the type of your struct to csvReader: > > struct Layout { string name; int value; double other; } > > auto readArrayOfStructs(string fname) { > Array!Layout res; > foreach(record; fname.readText.csvReader!Layout('\t')) { > res ~= record; > } > return res; > } Hi Deemon, Thank you, and sorry for the confusion, the requirement is as below auto reader(T) (Array!T T1s, T fname) { auto uFile = File(fName, "r"); foreach (record; uFile.byLineCopy().joiner("\n").csvReader!(Tuple!T1s)) // receive the type and fetch the record writeln(record); } void main () { auto fName = "C:\\Users\\bheev1\\Desktop\\Current\\Script\\Others\\Table1.csv"; struct T1 { string Name; string Country; int Age; } Array!T1 T1s; reader(fName, T1s); // pass the array Type as a function parameter } From, Vino.B |
January 09, 2018 Re: Storing struct in a array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vino | On Tuesday, 9 January 2018 at 17:41:10 UTC, Vino wrote: > On Tuesday, 9 January 2018 at 17:00:05 UTC, thedeemon wrote: >> On Tuesday, 9 January 2018 at 13:49:41 UTC, Vino wrote: >>> Hi All, >>> >>> It is possible to store struct in a array ans use the same in csvReader >> >> Sure, you can just pass the type of your struct to csvReader: >> >> struct Layout { string name; int value; double other; } >> >> auto readArrayOfStructs(string fname) { >> Array!Layout res; >> foreach(record; fname.readText.csvReader!Layout('\t')) { >> res ~= record; >> } >> return res; >> } > > Hi Deemon, > > Thank you, and sorry for the confusion, the requirement is as below > > auto reader(T) (Array!T T1s, T fname) { > auto uFile = File(fName, "r"); > foreach (record; uFile.byLineCopy().joiner("\n").csvReader!(Tuple!T1s)) // receive the type and fetch the record > writeln(record); > } > > void main () { > auto fName = "C:\\Users\\bheev1\\Desktop\\Current\\Script\\Others\\Table1.csv"; > struct T1 { string Name; string Country; int Age; } > Array!T1 T1s; > reader(fName, T1s); // pass the array Type as a function parameter > } > > > From, > Vino.B Details For example let say we have 3 struct auto read(T) (T Filename, T ArrayType) { T ArrayType res; foreach (record; Filename.byLineCopy().joiner("\n").csvReader!(T)(Tuple!ArrayType)) foreach(i, T; ColumnTypes) { res[i].insert(record[i]); } } return res; } void main () { struct S1 { } struct S2 { } struct S3 { } Get user input(UI) if(UI == S1) { Array!S1 T1; writeln(read(File1, Array Type)); } From, Vino.B |
January 09, 2018 Re: Storing struct in a array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vino | On Tuesday, 9 January 2018 at 18:09:58 UTC, Vino wrote: >>>> It is possible to store struct in a array ans use the same in csvReader >>> >>> Sure, you can just pass the type of your struct to csvReader: >> Array!T1 T1s; >> reader(fName, T1s); // pass the array Type as a function parameter First you write a template function that takes an array of some generic type and fills it with records from CSV file: void readData(DataType)(string fname, ref Array!DataType arr) { foreach (record; fname.readText.csvReader!DataType('\t')) { arr ~= record; } } Then you can use it in your main program with different types: struct S1 { string name; string value; int other; } struct S2 { int a; string b; } void main () { ... if (someCondition) { Array!S1 arr1; readData("data1.csv", arr1); } else { Array!S2 arr2; readData("data2.csv", arr2); } } A little advice. Kindly pause and spend an evening reading this book: http://ddili.org/ders/d.en/ Currently your code pieces look like a soup produced by someone who still confuses variables and types, and lacks basic programming skills. Read the book, don't rush with writing broken code. |
January 10, 2018 Re: Storing struct in a array | ||||
---|---|---|---|---|
| ||||
Posted in reply to thedeemon | On Tuesday, 9 January 2018 at 19:05:48 UTC, thedeemon wrote: > On Tuesday, 9 January 2018 at 18:09:58 UTC, Vino wrote: >>>>> It is possible to store struct in a array ans use the same in csvReader >>>> >>>> Sure, you can just pass the type of your struct to csvReader: > >>> Array!T1 T1s; >>> reader(fName, T1s); // pass the array Type as a function parameter > > First you write a template function that takes an array of some generic type and fills it with records from CSV file: > > void readData(DataType)(string fname, ref Array!DataType arr) { > foreach (record; fname.readText.csvReader!DataType('\t')) { > arr ~= record; > } > } > > Then you can use it in your main program with different types: > > struct S1 { string name; string value; int other; } > struct S2 { int a; string b; } > > void main () { > ... > if (someCondition) { > Array!S1 arr1; > readData("data1.csv", arr1); > } else { > Array!S2 arr2; > readData("data2.csv", arr2); > } > } > > A little advice. Kindly pause and spend an evening reading this book: > http://ddili.org/ders/d.en/ > > Currently your code pieces look like a soup produced by someone who still confuses variables and types, and lacks basic programming skills. Read the book, don't rush with writing broken code. Hi Deemon, I agree that my code is broken code and I am a Newbie in the world of programming, the confusion begin when i started writing the second module, and now i was able to find the issue, and the real requirement. The requirement is as below. Program: The below program works. auto reader(MyStruct) (File fname, auto ref MyStruct st) { alias ColumnTypes = AliasSeq!(MyStruct); foreach (record; fname.byLineCopy().joiner("\n").csvReader!(Tuple!ColumnTypes)(null)) { writeln(record[0], record[1], record[2]); } void main () { auto fname = File("C:\\Users\\bheev1\\Desktop\\Current\\Script\\Others\\Table2.csv"); auto Table = baseName(stripExtension(fname)); struct S1 { string Name; string Country; int Age; } S1 Table1s; reader(File(fname), join([Table, "1s"])); } The requirement is as below The function reader should store the data in column wise Array and return the same. Array!<Type has to be taken from ColumnTypes < Name of the array has to be taken from the header data as the CSV file has header Name, Country, Age using records.header) e.g Array!string Name; (records.header[0]); Array!string Country;(records.header[1]); Array!string Age;(records.header[2]); and return the above arrays From, Vino.B |
Copyright © 1999-2021 by the D Language Foundation