Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Lua Learning Notes IV (Basic Library in Lua)


May 12, 2021 Lua



The basic library of functions in Lua

Table 1

The basic library of functions

Function

Parameters

Note

assert(v[,mess age])

An assertion equivalent to C

v: When the expression v is nil or false will trigger an error,

Message: Information returned in the event of an error, default to "assertion failed!"

collectgarbage (opt [, arg])

is a common interface for garbage collectors, which is used to operate garbage collectors

opt: Action method flag

"Stop": Stop the garbage collector

"Restart": Restart the garbage collector

"Collect": Perform a full garbage collection cycle

"Count": Returns the amount of memory used in the current Lua in KB

"Step": Step through a garbage collection. S tep "Size" is specified by the parameter arg (large values require multiple steps to complete), and if you want to specify the step exactly, you need to experiment multiple times to achieve optimal results. If the step completes a collection loop, True is returned

Setpause: Sets the value of arg/100 as the time of the tentative collection

"Setstepmul": Set the value of arg/100 as an increase in step (i.e. new step s/old step s/arg/100)

dofile (filename)

Open and execute a lua block, and when the parameter fileme is ignored, the contents of the standard input device (stdin) are executed. R eturns the return value of all blocks. When an error occurs, dofile reflects the error to the caller

Note: Dofile cannot run in protection mode

error (message [, level])

Terminates the function being executed and returns the contents of the message as an error message (the error function never returns)

Typically, error will attach some information about the wrong location to the mess head.

The Level parameter indicates the location where the error was obtained.

Level-1 (default): for calling the error location (file-line number)

Level=2: Indicates which function calls the error function

Level=0: No incorrect location information is added

_G global environment table (global ( variable). )

The table that records the variable values of the global environment _G. _G

getfenv(f)

Returns the current environment table for function f

f can be the level of a function or call stack, level 1 is the current function, and level 0 or other values will return the value of the _G

getmetatable(object)

Returns the meta-table of the specified object.__metatable (if the meta-table of the object has a value, the value of the meta-table .__metatable of the object is returned), and nil is returned when the object does not have a meta-table

ipairs (t)

Returns three values Iterative function, table, 0

Key names and key value pairs that are used mostly for exhausting tables

e.g. for i, v in ipairs(t) do end

Each loop assigns the index level i and the key value to v

Note: This function can only be used for tables accessed in a numeric index such as: t ."1", "cash"

load (func [, chunkname])

Loading a function in a block, each call to func returns a string of words from the previous knot of the connection, and nil is returned at the end of the block

When no error occurs, a compiled block is returned as a function, otherwise nil is returned with an error message, and the environment of this function is global

Chunkname is used for error and debugging information

loadfile ([filename])

Similar to load, but loaded with the contents of a file or standard input (stdin) when fileame is not specified

loadstring (string [, chunkname])

Similar to load, but loaded with content is a string of words

e.g. assert (loadstring(s))()

next (table [, index])

Allows the program to traverse each field in the table, returning the next index and the value of that index.

Table: The table to traverse

index: The number in the first rope of the index to be returned, when index is nil, the value of the first index is returned, and when the index number is empty, the nil is returned when the index number is empty

Note: You can use next(t) to detect if the table is empty ( function can only be used for tables indexed in numbers similar to ipairs). )

ipairs (t)

Returns three values next function, table, and 0

Key names and key value pairs that are used mostly for exhausting tables

e.g. for n, v in pairs(t) do end

Each loop assigns the index level i and the key value to v

Note: This function can only be used for tables accessed by key name indexes such as: t-id="1", name="cash"

pcall (f, arg1, ···)

Call functions in protection mode (i.e. errors that occur will not be reflected to the caller)

When the calling function returns true as a function, the false message is returned when it fails

print (···)

Simply format the contents of the output parameters in a tostring manner

rawequal (v1, v2)

Detects whether v1 is equal to v2, and this function does not call any meta-table methods

rawget (table, index)

Gets the value of the index specified in the table, which does not call the method of any meta-table, successfully returns the corresponding value, and returns nil when the index does not exist

Note: This function can only be used for tables accessed in a numeric index such as: t ."1", "cash"

rawset (table, index, value)

Set the value of the specified index in the table, this function does not call any meta-table methods, and this function returns table

select (index, ···)

When index is a number, all index parameters greater than index are returned: e.g. select (2, "a", "b") returns "b"

The total number of parameters returned (excluding index) when index is "

setfenv (f, table)

Set the environment table for function f to table

f can be the level of a function or call stack, level 1 is the current function, and level 0 sets the environment table of the current thread

setmetatable (table, metatable)

The specified table sets the metameter metalable, cancels the table if the metalable is nil, and triggers an error when the metalable has __metatable field on it

Note: You can only specify LUA_TTABLE table for a table type

tonumber (e [, base])

Try converting parameter e to a number and returning nil when it cannot be converted

Base (2 to 36) indicates that parameter e is currently in use, the default is 10 percent, such as toumber (11,2)

tostirng(e)

Converting parameter e into a string, this function triggers the meta-table'__tostring event

type(v)

Returns the type name of the argument ("nil," "number," "string," "boolean," "table," "function," "thread," "userdata")

unpack (list [, i [, j]])

Returns the value of the index for the specified table, with i as the starting index and j as the end index

Note: This function can only be used for tables accessed in a , numeric index, otherwise only nil is returned such as: t "1", "cash"

_VERSION

Returns the current Lua version number "Lua 5.1".

xpcall (f, err)

Similar to pcall, functions are called in protection mode (i.e. errors that occur are not reflected to the caller)

However, you can specify a new error handler handle

When the calling function returns true as a function, the result returned by false pluserr is returned when it fails

Reference blog: http://www.cnblogs.com/whiteyun/archive/2009/08/12/1543184.html