Convert string to variable name or variable type
No, this is not possible. This sort of functionality is common in scripting languages like Ruby and Python, but C++ works very differently from those. In C++ we try to do as much of the program's work as we can at compile time. Sometimes we can do things at runtime, and even then good C++ programmers will find a way to do the work as early as compile time.
If you know you're going to create a variable then create it right away:
int count;
What you might not know ahead of time is the variable's value, so you can defer that for runtime:
std::cin >> count;
If you know you're going to need a collection of variables but not precisely how many of them then create a map or a vector:
std::vector<int> counts;
Remember that the name of a variable is nothing but a name — a way for you to refer to the variable later. In C++ it is not possible nor useful to postpone assigning the name of the variable at runtime. All that would do is make your code more complicated and your program slower.
C - Convert string to variable name
This is simply not possible in C.
Check out this question for more details.
How to return a variable name and assign the value of the variable returned in c
To quote Wichert, 'I suggest that you reconsider the problem you are trying to solve and check if there might not be a better method to approach it. Perhaps using an array, map or hash table might be an alternative approach that works for you.'
Convert a String to a Variable name in Python
I just cobbled a quick example to do what you want to do, I do not recommend that you do this, but I will assume that you have a good reason to and will therefore help.
vars = ["a", "b", "c"]
for i in range(len(vars)):
globals()[vars[i]] = (i+1)*10
print(a) # 10
print(b) # 20
print(c) # 30
This example may be a little complicated but it does exactly what you asked for in your question, long story short you can use the globals() function to turn a string into a variable. Link for more info here: https://www.pythonpool.com/python-string-to-variable-name/
Convert string to variable name in JavaScript
If it's a global variable then window[variableName]
or in your case window["onlyVideo"]
should do the trick.
Python convert string to variable name
Try the following:
lst = ['spam', 'eggs', 'ham']
d = {} # empty dictionary
for name in lst:
d[name] = rat.readColumn(ratDataset, name)
Do not use list
for your identifiers as it is a type identifier and you would mask its existence. The for loop can iterate directly for the elements inside -- no need to construct index and use it aganist the list. The d['spam']
will be one of your variables.
Although, it is also possible to create the real variable names like spam
, eggs
, ham
, you would not probably do that as the effect would be useless.
How to convert a string into a variable name?
you can convert a string with the name of a variable into a real variable using load()
:
load("return string")
example:
print(variable)
eh equals to:
print(load("return variable")())
in your case, instead of
if effects[i] == user_input then
use:
if effects[i] == load("return user_input")() then
you can also check if the method works with print:
print(load("return user_input")())
Convert string to variable name in python
x='buffalo'
exec("%s = %d" % (x,2))
After that you can check it by:
print buffalo
As an output you will see:2
Convert string to variable name in VBScript
If it is possible to put it into application or session you could do something like session(variableName) = 5
.
If not, the next easiest way is using a dictionary:
Dim myDict
Set myDict= CreateObject("Scripting.Dictionary")
myDict.Add (variableName, value)
Related Topics
Why Can't I Compile an Unordered_Map With a Pair as Key
How to Perform a Bitwise Operation on Floating Point Numbers
Why Does Const Imply Internal Linkage in C++, When It Doesn't in C
Best C++ Code Formatter/Beautifier
How to Efficiently Perform Double/Int64 Conversions With Sse/Avx
Why Does C++ Require a User-Provided Default Constructor to Default-Construct a Const Object
Why Does the C++ Stl Not Provide Any "Tree" Containers
Why Can't Variable Names Start With Numbers
Is Ncurses Available For Windows
Using Char* as a Key in Std::Map
Why Are References Not Reseatable in C++
How to Get the Index of an Iterator of an Std::Vector
Is There Any Real Risk to Deriving from the C++ Stl Containers
Create Registry Entry to Associate File Extension With Application in C++