# Dynamically created variablenames

**URL:** https://forum.kirupa.com/t/dynamically-created-variablenames/14660
**Category:** flash
**Created:** [March 1, 2003, 1:09am UTC](https://forum.kirupa.com/t/dynamically-created-variablenames/14660 "2003-03-01T01:09:25Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Pissmyran](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Pissmyran](https://forum.kirupa.com/u/Pissmyran)
#### Post date: [March 1, 2003, 1:09am UTC](https://forum.kirupa.com/t/dynamically-created-variablenames/14660/1 "2003-03-01T01:09:25Z")

</div>

Is it possible to have dynamically created variablenames in AS?

What I was thinking was something like this:

for (i = 0; i \< 5; i++) {  
“myVariable\_” + i = “Whatever”;  
}

After the loop is finished I want to have five variables named myVariable\_1, myVariable\_2, and so on.

## The problem is that I cant figure out how to acomplish this.

## “myVariable\_” + i = “Whatever”;

## …does not create a variable named myVariable\_1.

## eval(“myVariable\_” + i) = “Whatever”;

…doesnt seem to work either.

Any one have the solution to this, please?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 1, 2003, 1:14am UTC](https://forum.kirupa.com/t/dynamically-created-variablenames/14660/2 "2003-03-01T01:14:33Z")

</div>

```php
for(ii=1; ii<=5;ii++){
    _root["info"+ii] = variable
}

```

gotta use the \_root array…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 1, 2003, 1:38am UTC](https://forum.kirupa.com/t/dynamically-created-variablenames/14660/3 "2003-03-01T01:38:12Z")

</div>

btw you dont HAVE to use \_root… can be whatever scope you’re currently in (ore really any scope you want)

this[“info”+ii] = variable

you can also use the set function

set (“info”+ii, variable);

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 1, 2003, 1:39am UTC](https://forum.kirupa.com/t/dynamically-created-variablenames/14660/4 "2003-03-01T01:39:06Z")

</div>

Thanks a lot, Jubba 😃

Unfortunately, when one problem is solved, another one appears…

It seems that the variables that are dynamically created this way, are also dynamically deleted (perhaps at the same time as the other dynamic variable is created).

The problem now is that I need all the created variables to remain active at the same time, since they are loadVars-variables, with which I load text from textfiles to textfields.

* * *

## for (i = 1; i \< 5 + 1; i++){ _root["loadText_" + i] = new loadVars(); _root["textFil_" + i] = “MyTextFile\_” + i + “.txt”; _root["loadText_" + i].load(_root["textFil_" + i]); _root["loadText_" + i].onLoad = function() { \_root[“_root.txt_” + i].html = true; \_root[“_root.txt_” + i].htmlText = this.my\_text; } }

Well, the above code seem to actually load the text into the textfields for a second or so, but when the next loop kicks in, the text unloads again.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 1, 2003, 1:55am UTC](https://forum.kirupa.com/t/dynamically-created-variablenames/14660/5 "2003-03-01T01:55:19Z")

</div>

thanks sen. When I was trying to do something similar I had to use \_root[] to get it to work. so i assumed… 🙂

Not sure about the new problem tho…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 1, 2003, 3:43pm UTC](https://forum.kirupa.com/t/dynamically-created-variablenames/14660/6 "2003-03-01T15:43:29Z")

</div>

Thanks again for the help, you two 🙂 It taught me a couple of new AS-concepts.

## However, Im a bit unclear as to how…

## a) set(“info”+ii, variable);

## …and…

## b) this[“info”+ii] = variable;

…actually work.

## I mean, if I first initiate a variable with method a), how do I then refer to it? If I use…

## set(“info” + ii);

…wont I just create a new variable with the same name, but without any content (ie. clearing it, instead of refering to it)?

Do I have to use method b) to refer to the variable?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 1, 2003, 4:27pm UTC](https://forum.kirupa.com/t/dynamically-created-variablenames/14660/7 "2003-03-01T16:27:08Z")

</div>

set is a top level function that sets a variable with the given name (which can be dynamically defined) in the current major scope and assigns it the value passed.

set(“variableName”, value);

is the same as

variableName = value;

because its a ‘top level function’ set is only specific to the scope in which its called… the ‘major’ scope. By this I mean if you are writing the script in the main timeline, the variable will be set in \_root no matter what. You cant alter the scope at which set is set unless you write it directly within that scope. This means set only sets variables in movieclip objects since thats the only place you can physically write script. Within prototypes or within the function/method scope of other objects, set will still write the variable in the ‘major scope’ - the scope at which the script, as a whole, exists.

this[“info”+ii]

uses associative array referencing. Its just a way of referencing a variable (any scope) using strings (and therefore more dynamially) For more on that you can go see

[http://www.kirupaforum.com/showthread.php?s=&postid=85182#post85182](http://www.kirupaforum.com/showthread.php?s=&postid=85182#post85182)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [March 1, 2003, 4:52pm UTC](https://forum.kirupa.com/t/dynamically-created-variablenames/14660/8 "2003-03-01T16:52:51Z")

</div>

I feel a bit like GW Bush when reading your post (dyslectic, that is…or maby just plain stupid 😉 ), but if I understand you correctly I should be able to create two loadVars -variables (in my code above) with the set -method that will be active (ie. not deleted when the loop enters another loop) the whole time, and then just reference them with the \_root[] -array?

## Something like this, perhaps?

## for (i = 1; i \< 5 + 1; i++){ set(“loadText\_” + i, new loadVars()); set(“textFil\_” + i, “MyTextFile\_” + i + “.txt”); _root["loadText_" + i].load(_root["textFil_" + i]); _root["loadText_" + i].onLoad = function() { \_root[“_root.txt_” + i].html = true; \_root[“_root.txt_” + i].htmlText = this.my\_text; } }

This way, two persistent variables should be created at each loop (“loadText\_i”, and “textFil\_i”), or have I missed something?

EDIT: Well, I must have missed something, since I cant get this to work.
