Understanding Variables
From codeTank
Contents |
Introduction
It is absolutely essential that you learn and understand what a variable is. So pay attention :-P.
A variable is something you can store data in. This data varies from program to program, and usually changes during the execution of your program. It's for this reason that this data (that varies) is stored in a variable.
Example
Let's take a look at an example.
a = 14 print(a)
This is a pretty pointless script - but it does demonstrate how variables work. The first statement is setting the variable a equal to 14. So, a is the variable, and 14 is the data. The 14 is stored inside of a.
Then, we call the function print. We give print whatever is inside of the variable a, which happens to be 14. So the second line is translated to print(14), and is executed - which then displays 14 in the Output window.
Another Example
How about one more example.
hello_world = "Jimmy Urine" print(hello_world)
In this example, we see the first statement is setting the variable hello_world to the value of "Jimmy Urine". This stores the data, "Jimmy Urine", inside the variable hello_world.
The second statement, we call the function print. We give print whatever is inside of the variable hello_world, which happens to be "Jimmy Urine". The second line is translated to print("Jimmy Urine"), and is executed - which then displays Jimmy Urine in the Output window.
Variable Names
Notice how we can have variables that are larger than one character. In fact, we can name our variables almost whatever we want. The rules for creating a variable name are that it must contain only letters, numbers, and the underscore - AND it can't start with a number - AND it can't be a reserved keyword.
What's a reserved keyword? Well, any of these:
- and
- break
- do
- else
- elseif
- end
- false
- for
- function
- if
- in
- local
- nil
- not
- or
- repeat
- return
- then
- true
- until
- while
Illegal Examples
So, based on our rules, you can see how all the following examples are illegal variable names:
$hello = 1 -- cannot use the dollar sign 100punks = "rule" -- cannot start with a number hi mom = 34 -- cannot have a space while = "yes" -- cannot use a reserved keyword asdf% = 93 -- cannot use the percent symbol
Legal Examples
On the other hand, here are some legal examples:
hello = 1 _100punks = "rule" -- starts with an underscore, so it's legal hi_mom = 34 do_while = "yes" -- even though it includes keywords, it is not exactly a keyword -- so it is legal This_is_a_long_and_annoying_variable_name = 30 -- annoying, but legal
Variables are CaSe-SeNsItIvE
One more thing to note. Variables are case-sensitive. That means that you can't just swap out upper and lower case letters any time you want - if the variable isn't typed out EXACTLY as it has been, then Lua will assume you're talking about some other variable.
Here's an example:
While = 5 -- this is legal, because While is different than while a = 10 A = 30 print(a) -- will output 10 print(A) -- will output 30 print(While) -- will output 5
Overwriting a Variable
So how can we use these wonderful variables? Well, the first thing to note, is that variables can only hold one piece of data. That data can be really complicated - but it's still only one piece of data. Observe:
a = 14 a = "hi" print(a)
This program will output hi in the console. When Lua begins executing the script, it starts at the top. The first line will store the data 14 into the variable a. Lua will continue to the next line... and now it stores the data "hi" into the variable a - which completely overwrites the 14 that used to be in there. Finally, it prints the contents of a, which means it will output hi.
Once you overwrite a variable, the old data is lost, and is replaced by the new data. Please observe that this is different from mathematics, where things never really change state. A computer is a finite state machine - which basically means, as it continues to run, things inside of it change. The computer keeps changing, from one state to the next, always modifying itself. This is different than mathematics, where formulas are just formulas, and never change.
Data Types
There are different types of data. You may have noticed in the examples that we've been working with numbers and words, as our data. The words are actually called strings (in the programming world). They're named strings because they are a string of characters.
There are also six other types of data (in addition to numbers and strings). These are: nil, boolean, function, userdata, table, and thread. Every single variable will always hold data of one of these eight types.
Let me give you some examples again:
a = 10 -- this stores 10, a number, in the variable a b = "hey man" -- this stores "hey man", a string, in the variable b c = nil -- this stores nil, of type nil, in the variable c d = true -- this stores true, a boolean, in the variable d e = function() end -- this stores an empty function in the variable e f = window.create().hwnd -- this stores a userdata in the varible f g = {} -- this stores an empty table in the variable g h = coroutine.create(function() end) -- this stores a thread in the variable h
Ok. Some of that is probably confusing. Don't worry - look on the bright side - there are only eight data types. Once you learn about all eight, then you've got it all. And you already know about numbers and strings! So you're 25% done already!
Let's briefly talk about each type, so you get an idea of what each one is.
number
A number is just like it sounds. It can be a whole number, like 145, 12, or -3. Or it can have a decimal, like 3.14159265, or 1.3, or -0.33333. You can even use a form of scientific notation. 5e3 is a number - the e in the middle stands for "exponent". 5e3 is equal to 5 x 103, which is equal to 5000. Try it out: print(5e3) and see for yourself!
string
A string is a bunch of characters, stringed together. Strings are great for holding letters, words, or sentences. You indicate the beginning of a string using a double quote, single quote, or two brackets. You end the string the same way. Here are some examples of strings:
s = "" -- an empty string, which is legal t = 'Hi Mom!' -- a string using single quotes u = [[Whats up bizitch]] -- a string using brackets
nil
nil is a very special data type in Lua. There is only one value that is of type nil, and that is nil. Don't let this weirdness confuse you. nil exists to represent something that lacks data. All variables are set to nil before you use them - and when you use them the first time, that nil is overwritten with whatever data you're using. Just remember that nil basically means: nothing. If something is set to nil, then you are deleting the variable.
boolean
A boolean variable can be either true or false. This is important when we start talking about control structures. Basically, a boolean is just a flag. If it's not true, then it's false... and if it's not false, then it's true. A simple yes/no type of data.
function
A function is actually a data type. What does that mean? Well, in Lua, when you call a function (like print for example), it's important to realize that print is actually a variable - just like any other variable. This is an important feature of Lua that makes it very powerful. Some other languages don't treat functions as a data type, and it makes it more difficult to write programs in. Don't worry if you don't understand this just yet - it will become more apparent when we go over functions.
userdata
Userdata is something that you - as a Lua programmer - will never directly touch. It's used to track and interface with whatever is running the Lua script (like Brain Damage). Think of the problem: if we are going to create a window, how can we store that window in a variable? Well... we can't create a new data type willy-nilly. But we need SOMETHING. So, we store that window as a userdata. That way, you - as a Lua programmer - can still play with windows, without having a window data type. Basically, a userdata variable is something that you use to pass around to other functions, in order to keep track of some complicated data.
table
Tables are extremely important in Lua. This is the most important data type to understand (other than numbers and strings). A table, quite simply, holds a collection of other variables (possibly more tables). The ability to group variables into collections is very important, and we will go over it later.
thread
A thread is a special data type that is pretty advanced. As an analogy, observe your computer. You run multiple programs, and they all run at the same time, no problems. Well, in reality, they don't all run at once. What really happens is that one program is ran for a small duration (a few milliseconds), then it runs the next program for a little bit, then the next one. Your computer goes from program to program, running each one just a little bit, to give the illusion that they are all running at once. This is what threads are for - a thread is a part of your program that runs seemingly at the same time as another part of your program. In reality, it will never run at the exact same time, but it gives the illusion that it does. Don't worry about threads for a while - they aren't terribly important for beginners. Learn about the other seven data types before you learn about this one.
How to Use Variables
So how do you actually use a variable? I mean... what's the point?
Here is an example script that shows how you might use a variable:
name = prompt("What is your name?") alert("Hello, " .. name)
The first line will show a prompt for the user. The user sees the question, "What is your name?", and has a text field where he/she can type it in. Once they do that, whatever they enter in the text field is stored in the name variable.
Then, the program executes the second line. The double periods .. are used to add two strings together. So, it adds the string "Hello, ", to the string name, and then displays an alert window with that as the text.
I encourage you to play around with prompt, alert, and confirm. These three functions will give you a good understanding of how variables work, and help you build simple programs.

