# Functions from arrays

**URL:** <https://forum.kirupa.com/t/functions-from-arrays/262591>\
**Category:** Uncategorized\
**Created:** [June 7, 2008, 12:00pm UTC](https://forum.kirupa.com/t/functions-from-arrays/262591 "2008-06-07T12:00:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeancnicolas](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@jeancnicolas](https://forum.kirupa.com/u/jeancnicolas)\
**Post date:** [June 7, 2008, 12:00pm UTC](https://forum.kirupa.com/t/functions-from-arrays/262591/1 "2008-06-07T12:00:24Z")

</div>

Hello,  
I have 5 functions.  
I want one pulled out of an array and performed at random.

How could I code this? //im only using 2 funcs for space …

```auto

var way:Array = new Array("null","left")
var orientation:Number;
function null() {};
function left() {};
function direction()
{
orientation = Math.round(Math.random()*1);
way[orientation]; //gets the string > but how would I call it as a function!?
}
direction();

```

Thanks!!!

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [June 8, 2008, 12:17am UTC](https://forum.kirupa.com/t/functions-from-arrays/262591/2 "2008-06-08T00:17:14Z")

</div>

If you’re in a scope where _this_ is accessible, then you could use:

```auto
this[way[orientation]]();

```

However, I think that it makes more sense to avoid using strings:

```auto
var way:Array = new Array(_null, left);
var orientation:Number;
function _null() {
}
function left() {
}
function direction() {
	orientation = Math.round(Math.random() * 1);
	way[orientation]();
}
direction();

```

Also, you may want to avoid using _null_ as a function name because it is also the special value _null_.

---

<div class="post-metadata">

**Author:** ![jeancnicolas](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@jeancnicolas](https://forum.kirupa.com/u/jeancnicolas)\
**Post date:** [June 8, 2008, 10:45am UTC](https://forum.kirupa.com/t/functions-from-arrays/262591/3 "2008-06-08T10:45:35Z")

</div>

thanks - avoiding strings for function calls was the key for me. 🙂  
I also tried looking at Senocular’s class [Function Array](http://www.senocular.com/flash/actionscript.php?file=ActionScript_1.0/Classes/FunctionArray.as) which is pretty good…
