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.

262 questions
228
votes
9 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
  • 3,267
  • 3
  • 19
  • 25
111
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,531
  • 6
  • 34
  • 45
106
votes
8 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
  • 13,303
  • 18
  • 49
  • 71
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,061
  • 2
  • 9
  • 20
20
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
  • 16,871
  • 33
  • 101
  • 195
19
votes
4 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,603
  • 6
  • 34
  • 69
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,630
  • 15
  • 60
  • 82
16
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
  • 88,222
  • 31
  • 159
  • 162
14
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
  • 179
  • 1
  • 1
  • 6
12
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,756
  • 11
  • 33
  • 51
10
votes
1 answer

Why can't a sub access dynamic variables when used in a "return"ed map in raku?

It seems that a a sub cannot access dynamic variables when it is used inside a map and that map is "return"ed. Consider this piece of code: sub start { my $*something = 'foobar'; # WORKS say 'first say-something:'; …
Julio
  • 5,208
  • 1
  • 13
  • 42
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
  • 22,195
  • 68
  • 188
  • 269
8
votes
1 answer

Dynamic variables, CALLERS, Scalars, and assignment

I recently noticed that that re-initializing dynamic variables does not have the semantics I expected in most cases using assignment (binding works the way I expected it to, however). Specifically, in this code: sub g { my $*i = CALLERS::<$*i> …
codesections
  • 8,900
  • 16
  • 50
1
2 3
17 18