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.
Convert a String to Variable
Quick and dirty:
echo eval('return $'. $string . ';');
Of course the input string would need to be be sanitized first.
If you don't like quick and dirty... then this will work too and it doesn't require eval which makes even me cringe.
It does, however, make assumptions about the string format:
<?php
$data['response'] = array(
'url' => 'http://www.testing.com'
);
function extract_data($string) {
global $data;
$found_matches = preg_match_all('/\[\"([a-z]+)\"\]/', $string, $matches);
if (!$found_matches) {
return null;
}
$current_data = $data;
foreach ($matches[1] as $name) {
if (key_exists($name, $current_data)) {
$current_data = $current_data[$name];
} else {
return null;
}
}
return $current_data;
}
echo extract_data('data["response"]["url"]');
?>
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 in jquery
A common term for what you are trying to do is variable variables, or dynamic variables. As this question says:
There is almost always a better solution than using variable variables! Instead you should be looking at data structures and choose the right one for your problem.
You can easily store your data in an object, something like this:
// Master object to store all your variables
var variables = {};
$("#variable1, #variable2, ...").change(function() {
var id = $(this).attr('id'),
val = $(this).val();
// Store results in your master object
variables[id]=val;
// See what it looks like
console.dir(variables);
// See what it looks like
alert('variable1 is ' + variables['variable1'] + ', variable2 is ' + variables['variable2']);
});
Working JSFiddle.
How to convert string on variable in class?
Use the itemy
dictionary:
product_name = input("Product Name")
print(f'{GameShop.itemy[product_name]}'
The dictionary is really the only part of the GameShop
class that you need; I'd suggest not even making GameShop
a class, and just making a dict that's the source of truth for the item names and prices:
shop_prices = {
"ManaPotion": 50,
"HealthPotion": 50,
"StaminaPotion": 50,
}
Then you can print the prices with a loop like:
print("Shop prices:")
for item, price in shop_prices.items():
print(f"\t{item}: {price}")
and look up the price for a given item in shop_prices
by using the item's name as the key:
item = input("What do you want to buy?")
try:
print(f"That'll be {shop_prices[item]} gold pieces, please.")
except KeyError:
print(f"Sorry, {item} isn't in this shop.")
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 type of another variable
you can use Convert.ChangeType(s,param.GetType())
https://msdn.microsoft.com/en-us/library/dtb69x08(v=vs.110).aspx
or
ConvertTo(s,param.GetType())
https://msdn.microsoft.com/en-us/library/y13battt(v=vs.110).aspx
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.'
get a string and convert it to a variable and assign to a variable in cypress
You need to use .then()
to access the variable
cy.get('.ant-typography > span')
.invoke('text')
.then(parseInt)
.should('be.gt', 10)
.then(marks => {
cy.log('The value is '+ marks)
})
Can I return this "marks" value so I can use it outside the .then()
method?
See the docs Gleb references, once you have an asynchronous command (even just a .get()
) you are pretty much stuck with using a .then()
to access values derived from them.
Even an alias does not help - you need a .then()
to get it's value.
Related Topics
Removing Black Borders 4:3 on Youtube Thumbnails
How to Convert All Characters to Their HTML Entity Equivalent Using PHP
Check If All Values in Array Are the Same
Rationale Behind Simplexmlelement's Handling of Text Values in Addchild and Addattribute
JSON Parse Error with Double Quotes
PHP .Htaccess -> Pretty Url (In Reverse)
How to Serve Mp3 Files with PHP
Make Curl Output Stderr to File (Or String)
Prevent PHP Script from Being Flooded
PHP Authentication with Multiple Domains and Subdomains
PHP Artisan Migrate Throwing [Pdo Exception] Could Not Find Driver - Using Laravel
PHP Call Class Function by String Name
Laravel Pluck Fields from Relations
What Is the Postman-Token Header Attribute in Generated Code from Postman
Basic Simplexml Working Example