Lua: "Attempt to index a nill value" - lua

Hi so I just installed Lua and I have been playing around with it a bit. When I run a program that is supposed to calculate whether an integer is even or odd it throws an error at me.
Program:
function is_even(n)
if bit32.band(n,1) == 0 then
print('Even')
else
print('Odd')
end
end
This is the error that I receive:
stdin:2: attempt to index a nil value (global 'bit32')
stack traceback:
stdin:2: in function 'is_even'
(...tail calls...)
[C]: in ?
What am i doing wrong here? This program is supposed to work on Lua 5.2+ I currently have Lua 5.3.3 installed.

The bit32 library was deleted from Lua 5.3, because it now supports bitwise operators.

Related

Basic Quit Function in LOVE2D

I started using LOVE yesterday and I'm trying to code a basic quit function with LUA.
Here's my code
if function love.keyboard.getKey("q")
function love.event.quit()
end
I've tried it with and without the functions.
When I run it, it gives me this error
Error
Syntax error: main.lua:1: '(' expected near 'love'
Traceback
[C]: at 0x7ff9037828f0
[C]: in function 'require'
[C]: in function 'xpcall'
[C]: in function 'xpcall'
if function love.keyboard.getKey("q")
function love.event.quit()
end
Is invalid Lua syntax.
function is a keyword that is used to define function value. It is not part of the if statement and not used in function calls.
an if statement looks like
if condition then
-- block
end
love.keyboard.getKey("q") is not part of the love2d API.
What you want to do would probably be achieved by implementing a keypressed event handler.
Computer programs are not written by guessing some syntax and then asking for help.
Do a tutorial and read the Lua manual if you want to do anything useful with Lua.

Loadstring Error: attempted to call a nil value

loadstring("
\45\45\32\80\117\116\32\115\99\114\105\112\116\32\104\101\114\101\10\112\114\105\110\116\40\34\104\105\34\41\10")()
I keep getting an error stating this:
lua: /tmp/044957038/main.lua:12: attempt to call a nil value (global 'loadstring')
stack traceback:
/tmp/044957038/main.lua:12: in main chunk
[C]: in ?
Can anyone help me? (I’m using glot.io to run my script.)
Based on the comments and some testing in glot, this should work (the print() is just for reference):
print("\45\45\32\80\117\116\32\115\99\114\105\112\116\32\104\101\114\101\10\112\114\105\110\116\40\34\104\105\34\41\10")
load("\45\45\32\80\117\116\32\115\99\114\105\112\116\32\104\101\114\101\10\112\114\105\110\116\40\34\104\105\34\41\10")()
Output
-- Put script here
print("hi")
hi

Mediawiki scribunto lua module do not know builtin functions

I am having a problem with calling Lua built-in functions using Scribunto.
I created basic module Module:Item
local p = {};
function p.test(frame)
print("Hello World!")
end
return p
Which I call in different page as {{#invoke: Item | test}}
and I receive a following error:
Lua error in Module:Item at line 3: attempt to call global 'print' (a nil value).
Backtrace:
1. (tail call): ?
2. Module:Item:3: in function "chunk"
3. mw.lua:511: ?
4. (tail call): ?
5. [C]: in function "xpcall"
6. MWServer.lua:99: in function "handleCall"
7. MWServer.lua:313: in function "dispatch"
8. MWServer.lua:52: in function "execute"
9. mw_main.lua:7: in main chunk
10. [C]: ?
Since print is Lua built-in function I have the feeling the problem will be somewhere in setting on the pc.
However, when I imported wiki Infoboxes, they are working OK.
Versions:
Linux Mint Tara - Cinnamon based on ubuntu 18
MediaWiki 1.31.7
Scribunto (106fbf4) 17:24, 15 May 2018
Lua 5.1.5
Any help pointing where the problem can be is highly appreciated.
Scribunto intentionally doesn't include print. The "Removed functions and packages" section in its manual says this about it:
This was discussed on wikitech-l and it was decided that it should be omitted in favour of return values, to improve code quality. If necessary, mw.log() may be used to output information to the debug console.

Attempt to index global 'Grid' (a nil value)

I have been trying to run the following codes from Euler Equations Solver. It is a Lua script that I have been toying with using ZeroBrane Studio. But the project gave the following error every time I try to execute it.
attempt to index global 'Grid' (a nil value)
stack traceback:
...mming Codes\Project 1\Lua Script\Problem 1\Problem 1.lua:7: in main chunk
[C]: at 0x00401b00
I am completely new to Lua and I have been looking for the solutions everywhere. Appreciate any help that I can get.

Missing functions in Table Library in Lua

When I run table.maxn() or table.getn() in Lua I get the errors below:
> table.maxn(a)
stdin:1: attempt to call a nil value (field 'maxn')
stack traceback:
stdin:1: in main chunk
[C]: in ?
> table.getn(a)
stdin:1: attempt to call a nil value (field 'getn')
stack traceback:
stdin:1: in main chunk
[C]: in ?
When I try to explore the contents of the table object I get the results below. It is almost as though some functions are missing from the library.
> for k,v in pairs(table) do
>> print (k)
>> end
remove
insert
move
sort
concat
unpack
pack
>
I am using Lua5.3 - from downloaded win32 binaries > Lua53.exe
I have confirmed that I did not alter / affect the table object in any way. The results above were obtained after restarting the Interpreter afresh.
What could the issue be?
You are using lua 5.3 but:
table.getn was deprecated in lua 5.1 (ref)
table.maxn was deprecated in lua 5.2 (ref)
You need to write valid code for the version of lua you are targetting.

Resources