f = loadstring("return r(3,4)") --- :)On Sat, Jan 18, 2014 at 6:55 PM, H. van der Meer <H.vanderMeer@uva.nl> wrote: Thanks, that helped me running the code and I saw I should have used loadstring instead of load. But then the second hurdle arises:function r(value, repeats) return string.format("value = %d repeats = %3d", value, repeats)end f = loadstring("r(3,4)")print ("f() = <“, f(), ”>")Alas, this returns: f = < nil >Clearly the call to r doesn’t has the result I had in mind. What is my error here? Hans van der Meer On 18 jan. 2014, at 17:37, Victor Bombi <sonoro@telefonica.net> wrote: "The environment of the returned function is the global environment." http://www.lua.org/manual/5.1/manual.html#pdf-load ----- Original Message ----- From: H. van der Meer To: Lua mailing list Sent: Saturday, January 18, 2014 5:21 PMSubject: runtime execution fails why? 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) endfor i =1,10 dolocal f = load(t[i]) f()end endThis 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)?Thanks in advanceHans van der Meer
Thanks, that helped me running the code and I saw I should have used loadstring instead of load. But then the second hurdle arises:function r(value, repeats) return string.format("value = %d repeats = %3d", value, repeats)end f = loadstring("r(3,4)")print ("f() = <“, f(), ”>")Alas, this returns: f = < nil >Clearly the call to r doesn’t has the result I had in mind. What is my error here? Hans van der Meer On 18 jan. 2014, at 17:37, Victor Bombi <sonoro@telefonica.net> wrote: "The environment of the returned function is the global environment." http://www.lua.org/manual/5.1/manual.html#pdf-load ----- Original Message ----- From: H. van der Meer To: Lua mailing list Sent: Saturday, January 18, 2014 5:21 PMSubject: runtime execution fails why? 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) endfor i =1,10 dolocal f = load(t[i]) f()end endThis 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)?Thanks in advanceHans van der Meer
"The environment of the returned function is the global environment." http://www.lua.org/manual/5.1/manual.html#pdf-load ----- Original Message ----- From: H. van der Meer To: Lua mailing list Sent: Saturday, January 18, 2014 5:21 PMSubject: runtime execution fails why? 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) endfor i =1,10 dolocal f = load(t[i]) f()end endThis 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)?Thanks in advanceHans van der Meer
----- Original Message ----- From: H. van der Meer To: Lua mailing list Sent: Saturday, January 18, 2014 5:21 PMSubject: runtime execution fails why? 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) endfor i =1,10 dolocal f = load(t[i]) f()end endThis 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)?Thanks in advanceHans van der Meer