Re: runtime execution fails why?
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: runtime execution fails why?
- From: Dirk Laurie <dirk.laurie@...>
- Date: 2014年1月18日 18:40:08 +0200
2014年1月18日 H. van der Meer <H.vanderMeer@uva.nl>:
> Collecting function calls for example r(3,4) in a table I would like to
> execute these. But testing reveals I do something wrong.
> The program in essence:
>
> local function test(t)
> local function r(a,b) return string.format(“a = %d b = %d”, a, b) end
> for i =1,10 do
> local f = load(t[i])
> f()
> end
> end
>
> This fails with error message:
> /usr/local/bin/lua: [string "r(2,364)"]:1: attempt to call global 'r' (a nil
> value)
> stack traceback:
> [string "r(2,364)"]:1: in function 'f'
>
> What is wrong in my code? Why is function r considered global and not found
> in the scope of function test(t)?
load() resolves all references relative to the environment specified,
defaulting to _ENV. You can do this:
local function test(t)
local ENV={r=function(a,b) return string.format("a = %d b = %d", a, b) end}
...
local f = load(t[i],nil,nil,ENV)
...
end