Questions tagged [dynamic-variables]

In programming, a dynamic variable is a variable whose address is determined when the program is run. In contrast, a static variable has memory reserved for it at compilation time.

235 questions
377
votes
17 answers

Use dynamic variable names in JavaScript

In PHP you can do amazing/horrendous things like this: $a = 1; $b = 2; $c = 3; $name = 'a'; echo $$name; // prints 1 Is there any way of doing something like this with Javascript? E.g. if I have a var name = 'the name of the variable'; can I get a…
Finbarr
  • 28,231
  • 12
  • 59
  • 90
204
votes
8 answers

Using braces with dynamic variable names in PHP

I'm trying to use dynamic variable names (I'm not sure what they're actually called) But pretty much like this: for($i=0; $i<=2; $i++) { $("file" . $i) = file($filelist[$i]); } var_dump($file0); The return is null which tells me it's not…
user1159454
  • 2,987
  • 3
  • 17
  • 24
100
votes
7 answers

Assigning variables with dynamic names in Java

I'd like to assign a set of variables in java as follows: int n1,n2,n3; for(int i=1;i<4;i++) { n = 5; } How can I achieve this in Java?
Ashish Anand
  • 3,332
  • 6
  • 30
  • 44
92
votes
7 answers

Are Variable Operators Possible?

Is there a way to do something similar to either of the following: var1 = 10; var2 = 20; var operator = "<"; console.log(var1 operator var2); // returns true -- OR -- var1 = 10; var2 = 20; var operator = "+"; total = var1 operator var2; // total…
Gary
  • 11,083
  • 14
  • 43
  • 68
21
votes
2 answers

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

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…
hrickards
  • 1,001
  • 2
  • 9
  • 20
18
votes
5 answers

Replacing Text Inside of Curley Braces JavaScript

I am trying to use JavaScript to dynamically replace content inside of curly braces. Here is an example of my code: var myString = "This is {name}'s {adjective} {type} in JavaScript! Yes, a {type}!"; var replaceArray = ['name', 'adjective',…
Oliver Spryn
  • 15,563
  • 30
  • 91
  • 184
17
votes
5 answers

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

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…
julio
  • 6,132
  • 14
  • 55
  • 81
13
votes
3 answers

Can I create dynamic object names in JavaScript?

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
Terry Carnes
  • 169
  • 1
  • 1
  • 6
12
votes
4 answers

Javascript: Server sided dynamic variable names

How would I create dynamic variable names in NodeJS? Some examples say to store in the window variable, but I was assuming that is client-side Javascript. Correct me if I'm wrong.
hexacyanide
  • 76,426
  • 29
  • 148
  • 154
10
votes
6 answers

JavaScript Dynamic Variable Names

Ok so I want to create variables as a user clicks threw the code every click adds a new variable. I am currently using jquery and javascript I can't do it server side this must be done in the browser. newCount =…
Cody Weaver
  • 4,356
  • 8
  • 29
  • 50
10
votes
2 answers

value of integral type expected switch with dynamic parameter

Just out of curiosity. If I have the following Code public static string Format(dynamic exception) { switch (exception.GetType().ToString()) { case "test": return "Test2"; } return null; } i get the error "A…
10
votes
2 answers

Struts 2 dynamic variables

I'm trying to create a dynamic variable in Struts2 using set tag numConst will return a dynamic value that retrieved from database. For…
10
votes
5 answers

Dynamic variable in C#?

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#?
Moon
  • 20,835
  • 65
  • 174
  • 263
9
votes
3 answers

Evaluate dynamic variable name in ansible

I have vars where I put something like this: vars/main.yml hello_port: 80 world_port: 81 in my ansbile file I load the vars with vars_files: - ./vars/main.yml This is how I initialize m_name: - name: set_fact set_fact: m_name: …
Jordan Borisov
  • 1,427
  • 6
  • 30
  • 65
7
votes
2 answers

PHP join two variable names

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 =…
medk
  • 8,175
  • 16
  • 48
  • 71
1
2 3
15 16