How to Select a Variable by (String) Name

How to select a variable from a string containing its name?

You are definitely overcomplicating this. Just use a list of lists:

>>> import random
>>> num_arrays = 10
>>> array = [ [random.randint(1,10)] for i in range(num_arrays) ]
>>> array
[[2], [1], [10], [6], [9], [2], [2], [5], [8], [4]]

This way you can get the same functionality of each individual list and have it be extensible and not try to reference a variable by a concatenated string for its name.

selecting a variable string name in sql select query in case of stored procedures

I think following should work

SET @SQL = 'SELECT CID,DOB, NAME,'+''''+@Scname+''''+' from '+ @TableName

How to get the value of a variable given its name in a string?

If it's a global variable, then you can do:

>>> a = 5
>>> globals()['a']
5

A note about the various "eval" solutions: you should be careful with eval, especially if the string you're evaluating comes from a potentially untrusted source -- otherwise, you might end up deleting the entire contents of your disk or something like that if you're given a malicious string.

(If it's not global, then you'll need access to whatever namespace it's defined in. If you don't have that, there's no way you'll be able to access it.)

Set string to variable name

You can store the data in an object with the keys matching your select's values.

var data = {
basketball: "changeable-string",
handball: 760,
football: null,
baseball: "description: ball-game to play"
}

function myFunction(el) {
console.log(data[el.value])
}
<select name="box" onchange="myFunction(this);">
<option value="football">Football</option>
<option value="handball">Handball</option>
<option value="basketball">Basketball</option>
<option value="baseball">Baseball</option>
</select>

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.

Variable name as a string in Javascript

Typically, you would use a hash table for a situation where you want to map a name to some value, and be able to retrieve both.

var obj = { myFirstName: 'John' };obj.foo = 'Another name';for(key in obj)    console.log(key + ': ' + obj[key]);

Get variable by name from a String

Using reflection doesn't seem like a good design for what you're doing here. It would be better to use a Map<String, Integer> for example:

static final Map<String, Integer> VALUES_BY_NAME;
static {
final Map<String, Integer> valuesByName = new HashMap<>();
valuesByName.put("width", 5);
valuesByName.put("potato", 2);
VALUES_BY_NAME = Collections.unmodifiableMap(valuesByName);
}

Or with Guava:

static final ImmutableMap<String, Integer> VALUES_BY_NAME = ImmutableMap.of(
"width", 5,
"potato", 2
);

Or with an enum:

enum NameValuePair {

WIDTH("width", 5),
POTATO("potato", 2);

private final String name;
private final int value;

private NameValuePair(final String name, final int value) {
this.name = name;
this.value = value;
}

public String getName() {
return name;
}

public String getValue() {
return value;
}

static NameValuePair getByName(final String name) {
for (final NameValuePair nvp : values()) {
if (nvp.getName().equals(name)) {
return nvp;
}
}
throw new IllegalArgumentException("Invalid name: " + name);
}
}

How do I select variables in an R dataframe whose names contain a particular string?

If you just want the variable names:

grep("^[Bb]", names(df), value=TRUE)

grep("3", names(df), value=TRUE)

If you are wanting to select those columns, then either

df[,grep("^[Bb]", names(df), value=TRUE)]
df[,grep("^[Bb]", names(df))]

The first uses selecting by name, the second uses selecting by a set of column numbers.

FLUTTER How to get variable based on passed string name?

Dart when used in flutter doesn't support reflection.

If it's text that you want to have directly in your code for some reason, I'd advise using a text replace (using your favourite tool or using intellij's find + replace with regex) to change it into a map, i.e.

final Map<String, String> whee = {
'XVG': 'url 1',
'BTC': 'url 2',
};

Another alternative is saving it as a JSON file in your assets, and then loading it and reading it when the app opens, or even downloading it from a server on first run / when needed (in case the URLs need updating more often than you plan on updating the app). Hardcoding a bunch of data like that isn't necessarily always a good idea.

EDIT: how to use.

final Map<String, String> whee = .....

String getIconsURL(String symbol) {
//symbol = 'XVG'

return whee[symbol];
}

If you define it in a class make sure you set it to static as well so it doesn't make another each time the class is instantiated.

Also, if you want to iterate through them you have the option of using entries, keys, or values - see the Map Class documentation




Related Topics



Leave a reply



Submit