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.

Lua keyword/topics

Topics