Creating Structs/Classes at runtime?

Brian Brady brian.brady1982 at gmail.com
Sun Feb 17 20:48:08 PST 2013


On Monday, 18 February 2013 at 00:52:12 UTC, Adam D. Ruppe wrote:
[...]
>  -------
>  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"]);
>  }

doesn't this writefln() assume that I know what the Variables are 
called? ie "Name", "Occupation", etc.?
I want to be able to run the same program against a file with 4 
variables or a file with 400 variables, so specifying names 
wouldn't work.
Can I somehow use a record[var[a]] where a can be a number 
between 0 and the count of variables?

Also, if I wanted to store this as I read it in from csvReader, 
how can I define the array? It'll be of differing sizes if my 2 
csv's are of different sizes. This kinda the crux of my problem.

string[string] Data;
is my first guess based on http://dlang.org/hash-map.html

but then this doesn't work:
      foreach(line;file.byLine())
          foreach(ob;csvReader!Data(line))
          {
              // do things
          }



More information about the Digitalmars-d-learn mailing list