Here we have the code from Lua Performance Tips:

local sin = math.sin function foo (x) for i = 1, 1000000 do x = x + sin(i) end return x end print(foo(10)) 

to which luajit reports:

 stdin:3: attempt to call global 'sin' (a nil value) stack traceback: stdin:3: in function 'foo' stdin:1: in main chunk [C]: at 0x01000012d0 

also: write the table to a local variable:

 local a = {"abc", 123, true} 

then try to print her third key:

 print (a[3]) 

to which the interpreter again informs me:

 stdin:1: attempt to index global 'a' (a nil value) stack traceback: stdin:1: in main chunk [C]: at 0x01000012d0 

What am I doing wrong? Tell me, pliz?

    2 answers 2

    Obviously, you are using a live interface (via the command line). It compiles each function in its own environment, therefore local variables are not saved. Try to insert the code into the file, and then run.

    An alternative is to wrap a function:

     x = function() ваш код здесь end x() 

    UPD :

    Another option, the easiest:

     do ваш код здесь end 
    • one
      Shake your hand! From the file, everything works as it should. - sorda

    Isn't it easier to just remove the local prefix from the second example and instead of sin in the first example put math.sin ? Each line in the LuaJIT interpreter is executed in the same way as in the simple Lua interpreter - like do <строка> end . So no local 's in the lines if you want to use it in the next lines!