Storing struct in a array

Vino vino.bheeman at hotmail.com
Tue Jan 9 18:09:58 UTC 2018


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




More information about the Digitalmars-d-learn mailing list