print(rawequal(t, t2))-- nothing, __eq is not invoked
Copied!
pairs
Description: iterator to traverse over a table in any order possible
Usage: pairs(t)
Example:
1
local t ={1,2,3}
2
for k, v inpairs(t)do
3
print(k, v)
4
end
Copied!
ipairs
Description: iterator to traverse over a table in sequence
Usage: ipairs(t)
Example:
1
local t ={1,2,3}
2
for k, v inipairs(t)do
3
print(k, v)
4
end
Copied!
loadstring
Description: loads a Lua chunk
Usage: loadstring(string [, chunkName])
Example:
1
local f =loadstring([[return function() print(123) end]])
2
f()-- 123
Copied!
loadfile
Description: loads a Lua chunk from specified file, or from standard input if filename is not specified
Usage: loadfile([filename])
Example:
1
-- x.lua
2
returnfunction()print(123)end
3
​
4
-- other file.lua
5
local f =loadfile("x.lua")
6
f()-- 123
Copied!
next
Description: returns the next key, value pair in table starting from specified index, otherwise index, is nil
Usage: next(table [, index])
Example:
1
local t ={1,2,3}
2
print(next(t,2))-- 3, 3
3
print(next(t))-- 1, 1
Copied!
pcall
Description: calls a function in a protected state, returning any errors if they happen, otherwise returns true if successful plus the returned values from f
Usage: pcall(f, ...)
Example:
1
functionx(n)
2
return x + n
3
end
4
​
5
local success, ret =pcall(x,5)
6
print(success, ret)-- false C:\Users\user\lua_file.lua:2: attempt to perform arithmetic on a function value (global 'x')
Copied!
xpcall
Description: calls a function in a protected state, using err as the error handler and returning true if no errors happen, otherwise returns false plus the result from err
Usage: xpcall(f, err)
Example:
1
functionx(n)
2
return x + n
3
end
4
​
5
localfunctionx_error_handler(error)
6
print(error)
7
return123
8
end
9
​
10
local success, ret =xpcall(x, x_error_handler)
11
print(success, ret)
Copied!
Output:
1
C:\Users\user\lua_file.lua:2: attempt to perform arithmetic on a function value (global 'x')
2
false 123
Copied!
unpack
Description: unpacks a table in sequence, starting from i and ending with j (1, #table respectively by default), returning all values from it
Usage: unpack(table [, i [, j]])
Example:
1
local t ={1,2,3,4,5}
2
print(unpack(t))-- 1, 2, 3, 4, 5
3
print(unpack(t,2,4))-- 2, 3, 4
Copied!
type
Description: returns the data type of given value
Usage: type(value)
Example:
1
print(type(123))-- number
2
print(type({}))-- table
3
print(type(nil))-- nil
4
print(type(''))-- string
5
print(type(true))-- boolean
Copied!
tonumber
Description: converts value to a number if possible
Usage: tonumber(val [,base])
Example:
1
local x ='5'
2
print(tonumber(x))-- 5
3
print(type(tonumber(x)))-- number
4
print(type(x))-- string
Copied!
tostring
Description: converts value to a string
Usage: tostring(val)
Example:
1
print(tostring({}))-- table: 00ee88a8
2
print(tostring(5))-- 5
3
​
4
print('This is a boolean: '..tostring(true))-- This is a boolean: true