Setting variable value by the name

Started by
15 comments, last by iMalc 15 years, 5 months ago
I have a variable v1 in my program. Now I want to type in the variable name and value from the command prompt, and save the value for the variable given. Like this: >SET v1 4 And the program would this: v1=4; Basically I have to do now this now: if(variableNameGiven == "v1")then v1=givenVariableValue; But is it possible, somehow, to do it like this without any if's and stuff: variableNameGiven.value = givenVariableValue; Got it?
Advertisement
You can do that quite easily in SH, PHP, LISP and ECMAScript, and possibly also in Python and Perl. You can do that with a small effort in .NET and Java through reflection. In C and C++, you have to do the name-variable correspondence manually.
Ok, I'm doing it with C++ so I can't access variables directly if the name is given? Can i access the memory somehow?
The user writes "v1" and "lskjdf". You have to determine that "v1" is a variable (and what it corresponds to) but that "lskjdf" isn't a variable. In order to do this, you have to somehow have a list of variable names in your program.

The compiler deletes all variable names when it creates the program (although it will sometimes keep some of them in an implementation-dependent, configuration-dependent, hard-to-read-and-understand way). The closest you can get is creating a DLL which exports some functions that can be queried by name.

So, you're much better off writing these bindings by hand instead, or using an embedded Python interpreter, or using a script language instead of C++ for whatever you're doing (command-line PHP works pretty well and is fairly close to C++ in terms of syntax).
Not automatically. But there is almost certainly a way to do whatever it is you'd like to do. What is it you're trying to accomplish? An in-game console? A configuration file?
If your looking for something like Quakes console try taking a look at this article. It might give you some ideas.
You should just be using a simple lookup table like std::map. Have the key be a string (the variable name) and the value be whatever datatype you're using. From here you can lookup and create new values easily on the fly.
FTA, my 2D futuristic action MMORPG
How about having single character as the variable name in console?

int v1, v2, v7, v11;char v5, v6, v10;void *array[256];array['1'] = &v1array['2'] = &v2array['3'] = NULL; // ( I am not sure if you have to insert NULL lines or if compiler will do it for you. )array['4'] = NULL;array['5'] = &v5array['6'] = &v6array['7'] = &v7array['8'] = NULL;array['9'] = NULL;array['A'] = &v10array['B'] = &v11...char symbol = user input;some_variable value = user input;if(!(symbol >= '1' && symbol <= '9') && !(symbol >= 'A' && symbol <= 'B')){	// Invalid name}else{	void *a = array[symbol];	if(a != NULL){		*a = value;	}	else{		// Invalid name again	}}
How about:
std::map<std::string, boost::any>
You can use the preprocessor to create a table mapping the string name of the variable to the address of a variable. Then you just do a lookup in that and set the value.

TABLE_START(MyMappingTable, int)
TABLE_ENTRY(v1)
TABLE_ENTRY(v2)
TABLE_ENTRY(FooBar)
TABLE_END

Then you need to write the macros TABLE_START, TABLE_ENTRY, and TABLE_END. TABLE_ENTRY must stringify the variable name as well as take the address of the variable by that name, and each table entry will contain a const char*, plus say an int*. I believe the TABLE_ENTRY marco will contain the # and/or ## operator(s) which is not something I use very often. Someone will show you how to do that.
Of course this assumes that your variables are all of the same type, e.g. int, but you can name them whatever you like. There are ways around the one-type problem as well.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement