A custom name for variables

Steven Schveighoffer schveiguy at gmail.com
Thu May 28 21:04:57 UTC 2020


On 5/28/20 4:26 PM, Quantium wrote:
> I need to create a variable with custom name, like this
> import std;
> void main()
> {
>      string name;
>      readf(" %s", &name);
>      // some code that generates a variable of type integer and value 0
        int value = 0;
> }
> Could you help me with that?

If you are asking to have code that uses a runtime string as a variable 
name, you will not be able to do that in D. You have to know the name at 
compile time.

However, you can store data mapped to string names using an associative 
array:

int[string] variables;
variables[name] = 0;

// use like
writeln(variables[name])
variables[name] = 5;

-Steve


More information about the Digitalmars-d-learn mailing list