Initializing an associative array of struct

Denis noreply at noserver.lan
Sun Jun 14 04:36:09 UTC 2020


Hi,

I'm trying to combine a couple of general types (associative 
array, struct) in a compound data structure, but without success. 
Here is what I'm trying to achieve:

     "param" is a string variable
     "parameters[param].id" is an integer value
     "parameters[param].value" is a string value

param can be any of a fixed set of string values, all literals 
known at compile time.

parameters needs to be pre-populated with all of its keys during 
initialization, because they will be used to validate the param 
values that are read from a file (by checking if "param in 
parameters" is true).

I started with the following declarations:

     struct Parameter {
         int id;
         string value; }

     Parameter[string] parameters;
     string param;

But then I simply could not come up with a way to initialize the 
keys of parameters. I tried using assignments as well as enum, 
but always received either a compile or a runtime error. 
(Unfortunately the error messages did not enable me to find a 
solution.)

Note also that the defaults for id and value are fine (int and 
string defaults). But is it even possible to add the keys alone 
to parameters, without assigning something?

I would welcome a suggestion for how to initialize the keys of 
parameters. As there will be a couple dozen of the param string 
keys, a more succinct method would be preferable over a verbose 
one.

I realize that this is a very basic question, but I'm stumped!

Thank you in advance for your help.


More information about the Digitalmars-d-learn mailing list