Lua Programming/assignment

From Wikibooks, open books for an open world
Jump to: navigation, search

The assignment operator[edit]

In lua, as with most other programming languages, the *equals sign* acts as a dyadic *assignment operator* assigning the value of the expression of the right hand operand to the variable named by the left operand:

Assignment of variables[edit]

The following examples show the use of the equals sign for the assignment of variables:

fruit = "apple"   -- assign a string to a variable
count = 5         -- assign a numeric value to a variable

Strings and Numeric Values[edit]

Note that literal strings should be enclosed in quotationmarks to distinguish them from variable names:

apples = 5
favourite = "apples"   -- without quotes, apples would be interpreted as a variable name

Note that numeric values do not need to be enclosed in quotation marks and cannot be misinterpreted as a variable name, because variable names cannot begin with a numeral:

apples = 6    -- no quotes are necessary around a numeric parameter
pears = "5"   -- quotes will cause the value to be considered a string

Multiple Assignments[edit]

The lua programming language supports multiple assignments: