print(rawequal(t, t2))-- nothing, __eq is not invoked
pairs
Description: iterator to traverse over a table in any order possible
Usage: pairs(t)
Example:
local t ={1,2,3}
for k, v inpairs(t)do
print(k, v)
end
ipairs
Description: iterator to traverse over a table in sequence
Usage: ipairs(t)
Example:
local t ={1,2,3}
for k, v inipairs(t)do
print(k, v)
end
loadstring
Description: loads a Lua chunk
Usage: loadstring(string [, chunkName])
Example:
local f =loadstring([[return function() print(123) end]])
f()-- 123
loadfile
Description: loads a Lua chunk from specified file, or from standard input if filename is not specified
Usage: loadfile([filename])
Example:
-- x.lua
returnfunction()print(123)end
β
-- other file.lua
local f =loadfile("x.lua")
f()-- 123
next
Description: returns the next key, value pair in table starting from specified index, otherwise index, is nil
Usage: next(table [, index])
Example:
local t ={1,2,3}
print(next(t,2))-- 3, 3
print(next(t))-- 1, 1
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:
functionx(n)
return x + n
end
β
local success, ret =pcall(x,5)
print(success, ret)-- false C:\Users\user\lua_file.lua:2: attempt to perform arithmetic on a function value (global 'x')
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:
functionx(n)
return x + n
end
β
localfunctionx_error_handler(error)
print(error)
return123
end
β
local success, ret =xpcall(x, x_error_handler)
print(success, ret)
Output:
C:\Users\user\lua_file.lua:2: attempt to perform arithmetic on a function value (global 'x')
false 123
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:
local t ={1,2,3,4,5}
print(unpack(t))-- 1, 2, 3, 4, 5
print(unpack(t,2,4))-- 2, 3, 4
type
Description: returns the data type of given value
Usage: type(value)
Example:
print(type(123))-- number
print(type({}))-- table
print(type(nil))-- nil
print(type(''))-- string
print(type(true))-- boolean
tonumber
Description: converts value to a number if possible
Usage: tonumber(val [,base])
Example:
local x ='5'
print(tonumber(x))-- 5
print(type(tonumber(x)))-- number
print(type(x))-- string
tostring
Description: converts value to a string
Usage: tostring(val)
Example:
print(tostring({}))-- table: 00ee88a8
print(tostring(5))-- 5
β
print('This is a boolean: '..tostring(true))-- This is a boolean: true