dynamic-variables

Dynamic variable declaration in C

狂风中的少年 提交于 2019-12-01 09:41:42
问题 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 回答1: 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

How to access variable dynamically inside an anonymous function/closure?

随声附和 提交于 2019-12-01 06:58:57
To keep the global namespace clean, my JavaScript code is wrapped like this: (function() { /* my code */ })(); Now I have some variables declared in this scope which I want to access using a variable variable name (e.g. the name is 'something' + someVar ). In the global scope I'd simply use window['varname'] , but obviously that doesn't work. Is there a good way to do what I want? If not I could simply put those variables inside an object to use the array notation... Note: eval('varname') is not an acceptable solution. So please don't suggest that. This is a good question because this doesn't

Can I create dynamic object names in JavaScript? [duplicate]

送分小仙女□ 提交于 2019-11-30 23:40:18
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: javascript - dynamic variables Dynamic Javascript variable names I need to create a number of objects on a page and want to name them sequentially. Is there a way to do this in JavaScript? for (i=0;i<num;i++){ var obj+i = new myObject("param1","param2"); obj+i.someProperty = value; } This way I can dynamically create a varying number of objects (dependent on the value "num") and then set their properties

PHP join two variable names

自古美人都是妖i 提交于 2019-11-30 15:18:07
问题 I have a php script that gets a $_POST to decide which array to return. Ex: $n = $_POST['n']; // 1, 2 or 3 $a1 = array ('something', 'something else', 'another thing'); $a2 = array ('something 2', 'something else 2', 'another thing 2'); $a3 = array ('something 3', 'something else 3', 'another thing 3'); now I want to get the array that corresponds to the $n value, let's say "2" . How can I say echo $a . $n to get $a2 Thanks. 回答1: ${'a'.$n} gives you $a2 if $n is 2 . 回答2: It would be better

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

帅比萌擦擦* 提交于 2019-11-30 04:46:36
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 needed. In my case would I use something like @one = "21" input = "one" input = "@" + input changeto =

Dynamically Assign Variables in Matlab

泄露秘密 提交于 2019-11-28 14:11:37
I have the following function as part of a large codebase, which I inherited: function = save_function(fpath, a,b,c) save(fpath, 'a', 'b', 'c') end This function is called at the end of one script, before another script is to be executed. This way, the variable names are properly saved (bad design, I know - I didn't write this code). Now, I am making changes to the codebase, and realize that I need to store more variables in fpath . I face two options: Edit save_function to accept more inputs. This would break any other code in the codebase that also uses this function Write a save_function2(a

Calling dynamic variable in PowerShell

北城余情 提交于 2019-11-27 16:12:41
I am trying to create a new variable that would use other variable with dynamic name as its value. Here's what I am trying to do: I have a System.Array with two values: $Years = 2015, 2016 Another variable $Transactions has a list of various transactions. I am trying to use each of those $Years values in the following way: ForEach($Year in $Years){ New-Variable -Name "Transactions_$Year" -Value $Transactions | Where {$_.Date -like "*.$Year" Now what I would like to do (within that same ForEach-loop) is to use that $Transactions_$Year value when I am creating a another new variable, like this:

Dynamically Assign Variables in Matlab

我是研究僧i 提交于 2019-11-27 08:15:30
问题 I have the following function as part of a large codebase, which I inherited: function = save_function(fpath, a,b,c) save(fpath, 'a', 'b', 'c') end This function is called at the end of one script, before another script is to be executed. This way, the variable names are properly saved (bad design, I know - I didn't write this code). Now, I am making changes to the codebase, and realize that I need to store more variables in fpath . I face two options: Edit save_function to accept more inputs

Is there an easy way to create dynamic variables with Javascript?

烈酒焚心 提交于 2019-11-27 07:28:24
I've built a data-driven google map with different icons that get assigned to the map depending on the type of item located. So if I have 5 types of landmark, and each gets a different icon (store, library, hospital, etc.)-- what I'd like to do is generate the google icon objects dynamically. I was thinking something like this: types = array('hospital','church','library','store',etc); var i=0; while (i<=types.length) { var landmark + i = new google.maps.Icon(); landmark.image = "icon" + i + ".png"; i++; } however, as you've probably guessed, this doesn't work. I also tried using eval, like

Struts 2 dynamic variables

限于喜欢 提交于 2019-11-27 05:25:46
I'm trying to create a dynamic variable in Struts2 using set tag <s:set var="myNum" value="numConst" /> <s:set var="number" value="%{getText('@xxx.CommonConstant@'+#myNum)}" /> numConst will return a dynamic value that retrieved from database. For example, if the value is NINE then number should be @xxx.CommonConstant@NINE I have set the value in my java class so that @xxx.CommonConstant@NINE will return 9 . So far, the value can be displayed with no problem in text tag if I use <s:text name="%{getText(#number)}" /> It will return 9 but it displayed incorrectly when I tried using property tag