Storing struct in a array

Vino vino.bheeman at hotmail.com
Tue Jan 9 15:17:49 UTC 2018


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


More information about the Digitalmars-d-learn mailing list