Register forum user name Search FAQ

assignment

Assignment lets you "assign" (set) a variable to a value.

eg.


a = 42
b = "hello"


You can do multiple assignments, eg.


a, b = 42, "hello"


That does the same thing as the first example.

Multiple assignments are not generally used on their own (they can be confusing to read), except in collecting the results from a function. Functions can return multiple results, so multiple assignment is a useful way of getting those results. eg.


a, b, c = string.find ("the quick brown fox", "(a+)")


In this example, a will get the start character position of the match, b will get the end position, and c will get the capture.




With multiple assignment, if you (or the function) provides too few values, the extra ones are set to nil, eg.


a, b, c, d = 0, 1


This sets a to 0, b to 1, and c and d are both set to nil.




If you provide too many values, the extra ones are silently discarded, eg.


a = 1, 2, 3, 4, 5, 6


The values 2, 3, 4, 5, 6 are just discarded.


See Also ...

Lua keywords/topics

break
comments
data types
do
for (generic)
for (numeric)
function
identifiers
if / then / else
keywords
local
logical operators
precedence
relational operators
repeat
return
string literals
tables
while

Topics

Lua base functions
Lua bc (big number) functions
Lua bit manipulation functions
Lua coroutine functions
Lua debug functions
Lua io functions
Lua LPEG library
Lua math functions
Lua os functions
Lua package functions
Lua PCRE regular expression functions
Lua script extensions
Lua SQLite (database) interface
Lua string functions
Lua syntax
Lua table functions
Lua utilities
Scripting
Scripting callbacks - plugins

(Help topic: lua=assignment)

Documentation contents page


Search ...

Enter a search string to find matching documentation.

Search for:   

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.