Variables: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Anaminus
No edit summary
>Anaminus
No edit summary
Line 1: Line 1:
== Introduction ==
A '''variable''' is a name you can use to hold a value. In Lua, a variable can have any [[Data Types|data type]], such as a [[number]] or a [[string]].
A '''variable''' is a name you can use to hold a value. In Lua, a variable can have any [[Data Types|data type]], such as a [[number]] or a [[string]].


__TOC__
__TOC__
== Assignment ==
== Assignment ==
Assigning a value to a variable can be done using the '''=''' [[operator]], like so:
Assigning a value to a variable can be done using the '''=''' [[operator]], like so:
<pre>
<pre>
x = 10
x = 10
Line 14: Line 10:


Variable ''x'' now has a value of the number 10, and ''word'' has a value of the string "Hello". If we print out these variables, we should see the value they have:
Variable ''x'' now has a value of the number 10, and ''word'' has a value of the string "Hello". If we print out these variables, we should see the value they have:
<pre>
<pre>
print(x)    --> 10
print(x)    --> 10
print(word)  --> Hello
print(word)  --> Hello
</pre>
</pre>
=== Overwritting ===


A variable can be changed simply by assigning another value to it:
A variable can be changed simply by assigning another value to it:
<pre>
<pre>
n = 12
n = 12
Line 31: Line 23:
print(n)    --> 24
print(n)    --> 24
</pre>
</pre>
Even though ''n'' was assigned to a value of 12, it was overwritten by the second assignment of 24.
You can also use a variable as a value when assigning.
<pre>
n = 78
x = n
print(x)  --> 78
</pre>
When ''x'' was assigned to ''n'', it's value became the value of ''n''.


Even though ''n'' was assigned to a value of 12, it was overwritten by the second assignment of 24.
'''Remember!''' A ''variable'' is always on the '''left''' side of the ''='' while a ''value'' is on the '''right''' side.


== Names ==
== Names ==
The name you give a variable can only contain letters (uppercase and lowercase), numbers, or an underscore ( _ ). A variable name cannot be a reserved [[keyword]] or start with a number. Other than that, there are no restrictions. <!-- call me on this -->
The name you give a variable can only contain letters (uppercase and lowercase), numbers, or an underscore ( _ ). A variable name cannot be a reserved [[keyword]] or start with a number. Other than that, there are no restrictions. <!-- call me on this -->


Line 47: Line 50:
16th = "Lincoln"        -- NOT valid!
16th = "Lincoln"        -- NOT valid!
</pre>
</pre>
== Multiple Assignment ==
Lua allows you to assign more than one value to more than one variable.
<pre>
a, b, c = 1, 2, 3
print(a, b, c)    --> 1 2 3
</pre>
Each variable and each value is separated by a comma. Values to the right of the ''='' are assigned to their corresponding variables to the left. The first variable is assigned to the first value, the second variable is assigned to the second value, and so on. The above example is the same as this:
<pre>
a = 1
b = 2
c = 3
print(a, b, c)    --> 1 2 3
</pre>
=== Swapping ===
Multiple assignment is useful because it allows you to easily swap the values of two variables.
<pre>
x, y = 8, 36
print(x, y)    --> 8 36
x, y = y, x
print(x, y)    --> 36 8
</pre>
In this example, ''x'' is assigned to the original value of ''y'', and ''y'' is assigned to the original value of ''x''.


<!--
<!--

Revision as of 11:30, 28 September 2010

A variable is a name you can use to hold a value. In Lua, a variable can have any data type, such as a number or a string.

Assignment

Assigning a value to a variable can be done using the = operator, like so:

x = 10
word = "Hello"

Variable x now has a value of the number 10, and word has a value of the string "Hello". If we print out these variables, we should see the value they have:

print(x)     --> 10
print(word)  --> Hello

A variable can be changed simply by assigning another value to it:

n = 12
print(n)    --> 12

n = 24
print(n)    --> 24

Even though n was assigned to a value of 12, it was overwritten by the second assignment of 24.


You can also use a variable as a value when assigning.

n = 78
x = n

print(x)   --> 78

When x was assigned to n, it's value became the value of n.


Remember! A variable is always on the left side of the = while a value is on the right side.

Names

The name you give a variable can only contain letters (uppercase and lowercase), numbers, or an underscore ( _ ). A variable name cannot be a reserved keyword or start with a number. Other than that, there are no restrictions.

LETTERS = "ABC"          -- valid
x86 = 1978               -- valid
var_name = "variable"    -- valid
_ = "blank"              -- valid

if = 12                  -- NOT valid!
16th = "Lincoln"         -- NOT valid!

Multiple Assignment

Lua allows you to assign more than one value to more than one variable.

a, b, c = 1, 2, 3

print(a, b, c)    --> 1 2 3

Each variable and each value is separated by a comma. Values to the right of the = are assigned to their corresponding variables to the left. The first variable is assigned to the first value, the second variable is assigned to the second value, and so on. The above example is the same as this:

a = 1
b = 2
c = 3

print(a, b, c)    --> 1 2 3

Swapping

Multiple assignment is useful because it allows you to easily swap the values of two variables.

x, y = 8, 36
print(x, y)    --> 8 36

x, y = y, x

print(x, y)    --> 36 8

In this example, x is assigned to the original value of y, and y is assigned to the original value of x.