# Can actionscript do variable in variable?

**URL:** <https://forum.kirupa.com/t/can-actionscript-do-variable-in-variable/62643>\
**Category:** flash\
**Created:** [August 30, 2004, 5:50am UTC](https://forum.kirupa.com/t/can-actionscript-do-variable-in-variable/62643 "2004-08-30T05:50:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hiro](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@Hiro](https://forum.kirupa.com/u/Hiro)\
**Post date:** [August 30, 2004, 5:50am UTC](https://forum.kirupa.com/t/can-actionscript-do-variable-in-variable/62643/1 "2004-08-30T05:50:02Z")

</div>

I know in php etc its great with variables. You can use variables withinn variables to call another variable

e.g $somevar1, $somevar2

then $i = 1.

$$somevar$i (this will be treated as $somevar1)

Is it possible to do this in actionscript, because it saves one hell of alot of code.

---

<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:** [August 30, 2004, 8:48am UTC](https://forum.kirupa.com/t/can-actionscript-do-variable-in-variable/62643/2 "2004-08-30T08:48:12Z")

</div>

Sure you can. It’s called associative array referencing.

```auto

myvariable = "a nice variable";
firstpart = "my";
trace(this[firstpart+"variable"])

```

The expression in the [] will be evaluated first, and then used as name for the property/method/… to retrieve.

---

<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:** [August 30, 2004, 11:58pm UTC](https://forum.kirupa.com/t/can-actionscript-do-variable-in-variable/62643/3 "2004-08-30T23:58:25Z")

</div>

Awesome thanks!

---

<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:** [August 31, 2004, 2:41am UTC](https://forum.kirupa.com/t/can-actionscript-do-variable-in-variable/62643/4 "2004-08-31T02:41:01Z")

</div>

Actually some really weird stuff is happening now, i wonder if you can tell me why.

I have a variable content0, content1 etc. Which have data loaded in the first frame.

When i click this button it loads the corresponding content.

if i do.

```auto
_root.mainText.scrollBox.htmlText = content0;

```

That works fine, yet if i do

```auto
 
action = "0"; 
_root.mainText.scrollBox.htmlText = this["content" + action];

```

it comes up undefined! Unless i put a content0 = content0;  
before it.

Why would this happen it seems very silly to me!

Please note that content0 is a global variable from another frame, so maybe this has something to do with it?

---

<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:** [August 31, 2004, 2:43am UTC](https://forum.kirupa.com/t/can-actionscript-do-variable-in-variable/62643/5 "2004-08-31T02:43:09Z")

</div>

bleh… its funny how when you write a post it makes you think about it.

of course i needed to do

\_global[“content” + action]

NOT

this[“content” + action]

because its a global 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:** [August 31, 2004, 5:44am UTC](https://forum.kirupa.com/t/can-actionscript-do-variable-in-variable/62643/6 "2004-08-31T05:44:27Z")

</div>

Yep 🙂
