Jump to content

Scribunto: An Introduction/Variables

From Wikibooks, open books for an open world

In this chapter, we will learn about variables. A variable is a name that we give to a piece of data, so that we can use that data again.

Basic use

[edit | edit source]

The following program gives a simple example of how to use a variable. (Note: when you type this program in to the debug console, you will need to press Enter after the first line, not Shift + Enter. If you type in more than one line at once in the debug console, you can't use the = character on the start of a line to display the output.)

Code:

name = "Susan"
= name

Output:

Susan

In the first line of this program, we create a variable called name, and give it the value "Susan". On the second line, we display the contents of the name variable that we created.

You might have noticed that the equals sign = is being used for two different purposes in this program. This often confuses new Lua programmers, so let's make their difference clear now.

  • After a variable name, = means "store the value on the right in the variable on the left". This process is called assignment.
  • At the start of a new line in the debug console, = means "display the result of the statement on this line". This only works in the debug console, and doesn't work for multi-line statements.

Also, note that in Lua, a single equals sign = does not mean "equals". There is another operator for that, which we shall learn later.

Variable names

[edit | edit source]

In the previous example we used "name" as a variable name without giving any further explanation. Before you start creating your own variables, though, you should know that there are some restrictions on what names you can use. These restrictions are as follows:

  • Variable names must only use lower-case letters a to z, upper-case letters A to Z, digits from 0 to 9, and the underscore character _.
  • They must not begin with a number.
  • They must not be a word reserved for core Lua functions.

Here, "reserved words" are words used by the language itself for various different programming tasks. They are as follows:

  • and
  • break
  • do
  • else
  • elseif
  • end
  • false
  • for
  • function
  • if
  • in
  • local
  • nil
  • not
  • or
  • repeat
  • return
  • then
  • true
  • until
  • while

As long as you follow the rules above, you can use anything for a name. When you do choose variable names, though, it is best to be descriptive. Descriptive names are a lot easier to understand for other people looking at your programs (and for you looking at your own programs six months after you have written them). For example, if you are making a variable to store a wikilink, the names wikilink or link are easier to understand than wl or l.

Another important thing to be aware of is that variables are case-sensitive. As far as Lua is concerned, name, Name, and NAME are completely different variables.

Reusing variables

[edit | edit source]

Once you have assigned a value to a variable, you can use it as many times as you like.

Code:

name = "Susan"
= name
= name
= name

Output:

Susan
Susan
Susan

You can also use variables in calculations, and other kinds of Lua expressions. All you have to do is type in the variable name, and it will be treated as its value.

Code:

num = 3
= num * num

Output:

9

This can save you some keystrokes if you are dealing with long numbers or strings.

Code:

num = 4710462919837471
= num * num

Output:

2.2188460919164e+31

It is also the key to making your code reusable. We will learn more about this once we begin writing fully fledged Lua modules.

You can also assign new values to a variable as many times as you like. This is also called reassignment.

Code:

name = "Susan"
= name
name = "Tim"
= name
name = "Jennie"
= name

Output:

Susan
Tim
Jennie