Next: 6.2 String Manipulation Up: 6 Predefined Functions and Previous: 6 Predefined Functions and

6.1 Predefined Functions

dofile (filename)

This function receives a file name, opens it and executes its contents as a Lua chunk, or as pre-compiled chunks. When called without arguments, it executes the contents of the standard input. It returns 1 if there are no errors, nil otherwise. It issues an error when called with a non string argument.

dostring (string)

This function executes a given string as a Lua chunk. It returns 1 if there are no errors, nil otherwise.

next (table, index)

This function allows a program to traverse all fields of a table. Its first argument is a table and its second argument is an index in this table. It returns the next index of the table and the value associated with the index. When called with nil as its second argument, the function returns the first index of the table (and its associated value). When called with the last index, or with nil in an empty table, it returns nil .

In Lua there is no declaration of fields; semantically, there is no difference between a field not present in a table or a field with value nil . Therefore, the function only considers fields with non nil values. The order the indices are enumerated is not specified, even for numeric indices.

See Section 8.2 for an example of the use of this function.

nextvar (name)

This function is similar to the function next , but it iterates over the global variables. Its single argument is the name of a global variable, or nil to get a first name. Similarly to next , it returns the name of another variable and its value, or nil if there are no more variables. See Section 8.2 for an example of the use of this function.

tostring (e)

This function receives an argument of any type and converts it to a string in a reasonable format.

print (e1, e2, ...)

This function receives any number of arguments, and prints their values in a reasonable format. Each value is printed in a new line. This function is not intended for formatted output, but as a quick way to show a value, for instance for error messages or debugging. See Section 6.4 for functions for formatted output.

tonumber (e)

This function receives one argument, and tries to convert it to a number. If the argument is already a number or a string convertible to a number (see Section 4.2), it returns that number; otherwise, it returns nil .

type (v)

This function allows Lua to test the type of a value. It receives one argument, and returns its type, coded as a string. The possible results of this function are "nil" (a string, not the value nil ), "number" , "string" , "table" , "function" (returned both for C functions and Lua functions), and "userdata" .

Besides this string, the function returns a second result, which is the tag of the value. This tag can be used to distinguish between user data with different tags, and between C functions and Lua functions.

assert (v)

This function issues an ``assertion failed!'' error when its argument is nil .

error (message)

This function issues an error message and terminates the last called function from the library (lua_dofile , lua_dostring , ...). It never returns.

setglobal (name, value)

This function assigns the given value to a global variable. The string name does not need to be a syntactically valid variable name. Therefore, this function can set global variables with strange names like m v 1 or 34 . It returns the value of its second argument.

getglobal (name)

This function retrieves the value of a global variable. The string name does not need to be a syntactically valid variable name.

setfallback (fallbackname, newfallback)

This function sets a new fallback function to the given fallback. It returns the old fallback function.


Next: 6.2 String Manipulation Up: 6 Predefined Functions and Previous: 6 Predefined Functions and