Creating Structs/Classes at runtime?

Adam D. Ruppe destructionator at gmail.com
Sun Feb 17 16:52:11 PST 2013


There is a way to get what you described, and your code is fairly 
close... but there's a much simpler way too.

from the std.csv docs:

==

  When an input contains a header the $(D Contents) can be 
specified as an
  associative array. Passing null to signify that a header is 
present.

  -------
  auto text = "Name,Occupation,Salary\r"
      "Joe,Carpenter,300000\nFred,Blacksmith,400000\r\n";

  foreach(record; csvReader!(string[string])
          (text, null))
  {
      writefln("%s works as a %s and earns $%s per year.",
               record["Name"], record["Occupation"],
               record["Salary"]);
  }
  -------

==

The associative array directly is the simplest way to get this to 
work. All items read will be of type string if you do it like 
this, and you access with the [] syntax.

You can convert to other types with to when you want to use it:

import std.conv;

int salary = to!int(record["salary"]);



and that's the simplest way to make it work.


More information about the Digitalmars-d-learn mailing list