Storing struct in a array
thedeemon
dlang at thedeemon.com
Tue Jan 9 19:05:48 UTC 2018
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.
More information about the Digitalmars-d-learn
mailing list