Loading...
Preparing your coding drills
Comprehensive reference for 30 Lua methods across 6 categories.
table.insert
Inserts element value at position pos in list, shifting up other elements. The default value for pos is the length of the table plus one, so a call table.insert(t, x) inserts x at the end of table t.
table.remove
Removes from list the element at position pos, returning the value of the removed element. The default value for pos is the length of the table, so table.remove(t) removes the last element.
table.sort
Sorts the elements of list in place, from list[1] to list[#list]. If comp is given, it must be a function that receives two list elements and returns true when the first element must come before the second.
table.concat
Given a list where all elements are strings or numbers, returns the string list[i]..sep..list[i+1]...sep..list[j]. The default value for sep is the empty string, for i is 1, and for j is #list.
table.move
Moves elements from table a1 to table a2 (which can be the same table). Equivalent to: a2[t], a2[t+1], ... = a1[f], a1[f+1], ..., a1[e]. The default for a2 is a1.
# (length operator)
Returns the length of a table. For a sequence (table with consecutive integer keys starting at 1), it returns the number of elements. The length operator is not a function but a unary operator.
string.find
Looks for the first match of pattern in the string s. If found, returns the indices of s where the occurrence starts and ends; otherwise returns nil. If the pattern has captures, the captured values are also returned after the two indices.
string.gsub
Returns a copy of s in which all (or the first n) occurrences of the pattern have been replaced by a replacement string repl, which can be a string, table, or function. Also returns the total number of matches as a second value.
string.sub
Returns the substring of s that starts at i and continues until j. i and j can be negative, counting from the end of the string. The default value for j is -1 (end of string).
string.upper
Returns a copy of the string s with all lowercase letters changed to uppercase. The definition of what a lowercase letter is depends on the current locale.
string.lower
Returns a copy of the string s with all uppercase letters changed to lowercase. The definition of what an uppercase letter is depends on the current locale.
string.format
Returns a formatted version of its variable number of arguments following the description given in formatstring. Similar to the C sprintf function. Format directives include %d, %s, %f, %x, %q, and more.
math.max
Returns the argument with the maximum value, according to the Lua operator <. Requires at least one argument.
math.min
Returns the argument with the minimum value, according to the Lua operator <. Requires at least one argument.
math.floor
Returns the largest integral value less than or equal to x (rounds towards negative infinity).
math.ceil
Returns the smallest integral value greater than or equal to x (rounds towards positive infinity).
math.abs
Returns the absolute value of x. Works with both integers and floats.
math.random
When called without arguments, returns a pseudo-random float in the range [0, 1). When called with one integer m, returns a pseudo-random integer in the range [1, m]. When called with two integers m and n, returns a pseudo-random integer in the range [m, n].
print
Receives any number of arguments and prints their values to stdout, using the tostring function to convert each argument to a string. Arguments are separated by tabs and a newline is appended at the end.
io.open
Opens a file in the mode specified by the string mode. Returns a file handle, or nil plus an error message on failure. Mode strings include "r" (read), "w" (write), "a" (append), with optional "b" for binary mode.
io.write
Writes the value of each of its arguments to the default output file (stdout). Unlike print, io.write does not add tabs between arguments or a newline at the end, and it uses tostring on each argument.
type
Returns the type of its only argument, coded as a string. The possible results are "nil", "number", "string", "boolean", "table", "function", "thread", and "userdata".
tonumber
Tries to convert its argument to a number. If the argument is already a number or a string convertible to a number, it returns that number; otherwise returns nil. An optional base (2 to 36) specifies how to interpret the string.
tostring
Receives a value of any type and converts it to a string in a human-readable format. If the metatable of v has a __tostring field, tostring calls that metamethod with v as argument and uses the result.
pairs
Returns an iterator function, the table t, and nil, so that the construction "for k, v in pairs(t) do ... end" will iterate over all key-value pairs of table t. The order of traversal is not specified.
ipairs
Returns an iterator function, the table t, and 0, so that the construction "for i, v in ipairs(t) do ... end" will iterate over the pairs (1, t[1]), (2, t[2]), ..., up to the first absent index.
pcall
Calls the function f with the given arguments in protected mode. Any error inside f is not propagated; instead, pcall catches the error and returns a status code. Returns true plus all results of the call on success, or false plus the error object on failure.
xpcall
Similar to pcall, but sets msgh as the message handler. Any error inside f is not propagated; instead, xpcall calls the message handler with the original error object, allowing you to gather additional debug information (such as a traceback).
select
If index is a number, returns all arguments after argument number index. A negative number indexes from the end. If index is the string "#", returns the total number of extra arguments received.
unpack
Returns the elements from the given list. It is equivalent to: return list[i], list[i+1], ..., list[j]. By default, i is 1 and j is #list. In Lua 5.1, this function is a global called unpack; in 5.2+ it is table.unpack.