lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, May 3, 2013 at 12:14 PM, Laurent Faillie <l_faillie@yahoo.com> wrote:
> Hello,
>
> How to call a local function by it's name ?
>
> Something like
>
> ----
> local function normMilli( v ) return v/1000 end
>
> local name = 'normMilli'
> ----
> Then I need the call the function for which the name is stored in name
> variable.
>
> I know for global function, I can use _G, but I didn't find for local ones ?
>

Can't you just store these functions inside a table instead of in
local variables?

local named_funcs = {}
named_funcs.normMilli = function ( v ) return v/1000 end
local name = 'normMili'
print(named_funcs[name](5e3))

-- 
NI!

() - www.asciiribbon.org
/\ - ascii ribbon campaign against html e-mail and proprietary attachments