dynamic-variables

Using the value of a variable as another variables name in Ruby

徘徊边缘 提交于 2019-12-18 11:45:31
问题 I'm just starting out in learning Ruby and I've written a program that generates some numbers and assigns them to variables @one, @two, @three etc. The user can then specify a variable to change by inputting it's name (e.g one). I then need to do something like '@[valueofinout] = asd'. How would I do this, and is there a better way as the way I'm thinking of seems to be discouraged? I've found x = "myvar" myvar = "hi" eval(x) -> "hi" but I don't completely understand why the second line is

Dynamic variable in C#?

时光总嘲笑我的痴心妄想 提交于 2019-12-17 07:37:05
问题 Is it possible to use a dynamic variable (not sure about naming) in C#? In PHP, I can do $var_1 = "2"; $var_2 = "this is variable 2"; $test = ${"var_".$var_1}; echo $test; output: this is variable 2; Can we do this in C#? 回答1: In C#, you use dictionaries to associate values with strings. 回答2: No, basically. The compiler doesn't guarantee that method variables will exist (in their form as written), or with names... If they were fields (instance or static), then you could use reflection to get

How to create dynamic fields in Google App Engine expando class?

有些话、适合烂在心里 提交于 2019-12-06 05:14:49
问题 I have a db expando class called widget. I'm passing in a json string and converting it to a dict and then adding it to the datastore. My question is how can I loop through my dict to create dynamic fields. widget = Widget.get_by_key_name(key_name) widget.name = self.request.get('wname') fields = simplejson.loads(self.request.get('wcontents')) for k,v in fields.iteritems(): widget.k = v This renders "k" as my field name as oppose to the k value in the dict. 回答1: The syntax widget.k references

How to create dynamic fields in Google App Engine expando class?

試著忘記壹切 提交于 2019-12-04 10:38:51
I have a db expando class called widget. I'm passing in a json string and converting it to a dict and then adding it to the datastore. My question is how can I loop through my dict to create dynamic fields. widget = Widget.get_by_key_name(key_name) widget.name = self.request.get('wname') fields = simplejson.loads(self.request.get('wcontents')) for k,v in fields.iteritems(): widget.k = v This renders "k" as my field name as oppose to the k value in the dict. The syntax widget.k references the attribute k on object widget . To dynamically choose which attribute you set, use the built-in setattr

How to create variables with dynamic names in C#?

落爺英雄遲暮 提交于 2019-12-02 07:45:19
问题 I want to create a var in a for loop, e.g. for(int i; i<=10;i++) { string s+i = "abc"; } This should create variables s0, s1, s2... to s10. 回答1: You probably want to use an array. I don't know exactly how they work in c# (I'm a Java man), but something like this should do it: string[] s = new string[10]; for (int i; i< 10; i++) { s[i] = "abc"; } And read http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx 回答2: Your first example wouldn't work in any language as you are trying to

How to create variables with dynamic names in C#?

百般思念 提交于 2019-12-02 05:51:30
I want to create a var in a for loop, e.g. for(int i; i<=10;i++) { string s+i = "abc"; } This should create variables s0, s1, s2... to s10. Frans You probably want to use an array. I don't know exactly how they work in c# (I'm a Java man), but something like this should do it: string[] s = new string[10]; for (int i; i< 10; i++) { s[i] = "abc"; } And read http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx Your first example wouldn't work in any language as you are trying to redefine the variable "i". It's an int in the loop control, but a string in the body of the loop. Based on your

Dictionary use instead of dynamic variable names in Python

耗尽温柔 提交于 2019-12-02 01:14:08
I have a long text file having truck configurations. In each line some properties of a truck is listed as a string. Each property has its own fixed width space in the string, such as: 2 chracters = number of axles 2 characters = weight of the first axle 2 characters = weight of the second axle ... 2 characters = weight of the last axle 2 characters = length of the first axle spacing (spacing means distance between axles) 2 characters = length of the second axle spacing ... 2 characters = length of the last axle spacing As an example: 031028331004 refers to: number of axles = 3 first axle

PHP take string and check if that string exists as a variable

╄→尐↘猪︶ㄣ 提交于 2019-12-01 22:18:31
I have an interesting situation. I am using a form that is included on multiple pages (for simplicity and to reduce duplication) and this form in some areas is populated with values from a DB. However, not all of these values will always be present. For instance, I could be doing something to the effect of: <?php echo set_value('first_name', $first_name); ?> and this would work fine where the values exist, but $user is not always set, since they may be typing their name in for the first time. Yes you can do isset($first_name) && $first_name inside an if statement (shorthand or regular) I am

PHP take string and check if that string exists as a variable

淺唱寂寞╮ 提交于 2019-12-01 21:51:20
问题 I have an interesting situation. I am using a form that is included on multiple pages (for simplicity and to reduce duplication) and this form in some areas is populated with values from a DB. However, not all of these values will always be present. For instance, I could be doing something to the effect of: <?php echo set_value('first_name', $first_name); ?> and this would work fine where the values exist, but $user is not always set, since they may be typing their name in for the first time.

Dynamic variable declaration in C

早过忘川 提交于 2019-12-01 10:53:49
I'm a ruby developer and its been long since I've coded in C. I have this small issue -Basically I want to use a datatype in C which behaves like a symbol in C. In other words, is this possible in C? Program asks user for name user replies - "foobar" declare an integer with the same name i.e. int foobar Thanks Unlike in interpreted languages, C does not have a dictionary of variable names at runtime. There exist no variable names at runtime at all. Hence unfortunately it is impossible to do what you want in C. It's not possible to do this in C without implementing your own symbol table to