Some basic functions of Lua

Source: Internet
Author: User

Copyright statement: Reprinted with a hyperlinkArticleSource and author information and this statement
Http://cjbskysea.blogbus.com/logs/48140313.html

The basic function library is the built-in function library of Lua and does not require additional loading.

1. Assert (V [, message])
Function: equivalent to the assertion of C,
Parameters:
V: When expression V is nil or false, an error is triggered,
Message: the message returned when an error occurs. The default value is "assertion failed! "

--------------------------------------------------------------------------------

2. collectgarbage (OPT [, Arg])
Function: a common interface used to operate the garbage collector.
Parameters:
OPT: Operation Method flag
"Stop": Stop the Garbage Collector
"Restart": restart the garbage collector.
"Collect": execute a full garbage collection cycle
"Count": returns the amount of memory used in the current Lua (in KB)
"Step": Perform garbage collection in one step. the step size is specified by the ARG parameter (a large value can be completed in multiple steps). To accurately specify the step size, multiple experiments are required to achieve the optimal effect. True is returned if the step is collected once.
"Setpause": sets the value of Arg/100 as the duration of the tentative collection.
"Setstepmul": sets the ARG/100 value as the step increase (I .e., the new step = the old step * Arg/100)

--------------------------------------------------------------------------------

3. dofile (filename)
Function: open and execute a Lua block. When the filename parameter is ignored, the content of the standard input device (stdin) is executed. Returns the return value of all blocks. When an error occurs, dofile returns the error to the caller.
Note: dofile cannot be run in protected mode.

--------------------------------------------------------------------------------

4. Error (Message [, level])
Function: Terminate the function being executed and return the message content as an error message (the error function will never return)
Generally, an error is appended to the message header.
The level parameter indicates the location where the error is obtained,
Level = 1 [Default]: indicates the call error location (File + row number)
Level = 2: indicates the function that calls the error function.
Level = 0: no error location information is added.

--------------------------------------------------------------------------------

5. _ G Global Environment table (global variable)
Function: records the global environment variable value table _ g. _ g = _ g

--------------------------------------------------------------------------------

6. getfenv (f)
Function: returns the current environment table of function f.
Parameter: F can be a function or call stack level. Level 1 [Default] is the current function. Level 0 or other values will be returned to the global environment _ g.

--------------------------------------------------------------------------------

7. getretriable (object)
Function: returns the metadata table of the specified object (if the metadata table of the object is. if the _ retriable item has a value, the meta table of the object is returned. _ resumable value). If the object does not have a metadata table, Nil is returned.

--------------------------------------------------------------------------------

8. ipairs (t)
Function: returns three Value Iteration functions, tables, and 0.
Key names and key-value pairs for the exhaustive table
For example: for I, V in ipairs (t) Do
<Body>
End
Each cycle assigns the index level I and the key value to V
Note: This function can only be used for tables accessed by digital indexes, such as: t = {"1", "cash "}

--------------------------------------------------------------------------------

9. Load (func [, chunkname])
Function: load a function in a block. Each call to func will return a string that is connected to the previous knot, and return nil at the end of the block.
If no error occurs, a compiled block is returned as a function. Otherwise, Nil is returned with an error message. The environment of this function is the global environment.
Chunkname is used for error and debugging information.

--------------------------------------------------------------------------------

10. LoadFile ([filename])
Function: similar to load, but it loads a file or when no filename is specified, it loads the content of the standard input (stdin ).

--------------------------------------------------------------------------------

11. loadstring (string [, chunkname])
Function: similar to load, but the content loaded is a string
For example, assert (loadstring (s ))()

--------------------------------------------------------------------------------

12. Next (Table [, Index])
Function: AllowProgramTraverse each field in the table and return the value of the next index and the index.
Parameter: Table: The table to be traversed
Index: number in the first index of the index to be returned. If index is nil [], the value of the first index is returned, if the index number is the last index or the table is empty, Nil is returned.
Note: You can use next (T) to check whether the table is empty (this function can only be used for digitally indexed tables similar to ipairs)

--------------------------------------------------------------------------------

13. ipairs (t)
Function: returns three values: Next function, table, and 0.
Key names and key-value pairs for the exhaustive table
For example, for N, V in pairs (t) Do
<Body>
End
Each cycle assigns the index level I and the key value to V
Note: This function can only be used for tables accessed with key names, such as: t = {id = "1", name = "cash "}

--------------------------------------------------------------------------------

14. pcall (F, arg1 ,···)
Function: Call a function in Protected Mode (that is, errors will not be reflected to the caller)
If the function is successfully called, true is returned. If the function fails, false and an error message are returned.

--------------------------------------------------------------------------------

15. Print (···)
Function: format the output parameters in tostring mode.

--------------------------------------------------------------------------------

16. rawequal (V1, V2)
Function: checks whether V1 is equal to V2. This function does not call any metadata table method.

--------------------------------------------------------------------------------

17. rawget (table, index)
Function: gets the value of the specified index in the table. This function does not call any metadata table method and returns the corresponding value. If the index does not exist, Nil is returned.
Note: This function can only be used for tables accessed by digital indexes, such as: t = {"1", "cash "}

--------------------------------------------------------------------------------

18. rawset (table, index, value)
Function: sets the value of the specified index in the table. This function does not call any metadata table method. This function returns table

--------------------------------------------------------------------------------

19. Select (index ,···)
Function: If index is a number, all parameters with index greater than index are returned, for example, select (2, "A", "B"
If the index is "#", the total number of parameters (excluding the index) is returned)

--------------------------------------------------------------------------------

20. setfenv (F, table)
Function: Set the environment table of function f to table.
Parameter: F can be a function or call stack level. Level 1 [Default] is the current function. Level 0 sets the environment table of the current thread.

--------------------------------------------------------------------------------

21. setretriable (table, retriable)
Function: sets the metadatabase retriable for the specified table. If the retriable is nil, the metadatabase for the table is canceled. If the retriable field has the _ retriable field, an error is triggered.
Note: Only the table metadata can be specified for the lua_ttable table type.

--------------------------------------------------------------------------------

22. tonumber (E [, base])
Function: Convert the parameter E to a number. If the conversion fails, Nil is returned.
Base (2 ~ 36) indicates the current hexadecimal format used by parameter E. The default value is 10 hexadecimal, for example, tonumber (11,2) = 3.

--------------------------------------------------------------------------------

23. tostirng (E)
Function: converts parameter E to a string. This function triggers the _ tostring event of the Meta table.

--------------------------------------------------------------------------------

24. Type (V)
Function: type name of the returned parameter ("nil", "Number", "string", "Boolean", "table", "function", "Thread", "userdata ")

--------------------------------------------------------------------------------

25. Unpack (list [, I [, J])
Function: returns the index value of the specified table. I is the starting index and J is the ending index.
Note: This function can only be used for tables accessed by numerical indexes. Otherwise, only nil such as: t = {"1", "cash"} is returned "}
--------------------------------------------------------------------------------

26. _ version
Function: returns the current Lua version number "Lua 5.1 ".

--------------------------------------------------------------------------------

27. xpcall (F, err)
Function: similar to pcall, a function is called in protection mode (that is, errors will not be reflected to the caller)
However, you can specify a new handle to the error handling function.
If the function is successfully called, true is returned. If the function fails, false and err are returned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.