# Tables of Functions

7 messages
Open this post in threaded view
|

## Tables of Functions

 ```I would like to make an array of functions. I tried the following syntax. function steps[1] (a,b) print("steps[1]",a,b) end This yields the error message: lua: test.lua:1: '(' expected near '[' If I write this instead, it works. steps[1] = function (a,b) print("steps[1]",a,b) end Am I justified in expecting the first flavor to work? Thank you, Ken Smith ```
Open this post in threaded view
|

## Re: Tables of Functions

 ```> Am I justified in expecting the first flavor to work? No. The reference manual says funcname ::= Name {'.' Name} [':' Name] See http://www.lua.org/manual/5.1/manual.html#2.5.9 --lhf ```
Open this post in threaded view
|

## Re: Re: Tables of Functions

 ```On 9/25/06, Luiz Henrique de Figueiredo <[hidden email]> wrote: ``````> Am I justified in expecting the first flavor to work? No. The reference manual says funcname ::= Name {'.' Name} [':' Name] `````` In that case, I might expect the following to work but it doesn't. function steps.1 (a,b) print("steps[1]",a,b) end Can you tell me why? Thank you, Ken Smith ```
Open this post in threaded view
|

## Re: Tables of Functions

 ``` On 25 Sep 2006, at 16:49, Ken Smith wrote: ```On 9/25/06, Luiz Henrique de Figueiredo <[hidden email]> wrote: ```> Am I justified in expecting the first flavor to work? No. The reference manual says funcname ::= Name {'.' Name} [':' Name] `````` In that case, I might expect the following to work but it doesn't. function steps.1 (a,b) print("steps[1]",a,b) end Can you tell me why? `````` ```name as defined above has to be a valid identifer. 1 doesn't count. For instance, ``` function steps.foo(a, b) is a valid alias for steps[ "foo" ] = function(a, b) -Eric ```
Open this post in threaded view
|

## Re: Re: Tables of Functions

 ```Right. Ok. I can use the non-syntactic sugar version to create my list of functions. Thank you for the responses. Ken Smith ```
 ```On Mon, Sep 25, 2006 at 04:58:59PM -0700, Ken Smith wrote: > Right. Ok. I can use the non-syntactic sugar version to create my > list of functions. Thank you for the responses. Or just list = { function() return "a" end, function() return "b" end, function() return "c" end, } print(list[2]()) -- Glenn Maynard ```
 In reply to this post by Ken Smith-2 ```On Mon, Sep 25, 2006 at 04:58:59PM -0700, Ken Smith wrote: > Right. Ok. I can use the non-syntactic sugar version to create my > list of functions. Thank you for the responses. You may know, but if defining functions in order ([1], [2], ...) you also can do: vtable = { function (x) print(x) end, function (y) print(y*2) end, -- ... } t[2](5) ```