If you are just starting to program with Lua, welcome. This is part 2 of Programming Lua series.
In this short tutorial, I am going to demonstrate how to create variables to store data in Lua.
Launch Lua REPL
Lua ships with a program called REPL (REPL stands for Read Evaluate Print Loop) where you can try out Lua code. So, let’s launch the Lua REPL by running the lua command. When you invoke the Lua REPL, you should see something like this
✗ lua
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
>
Creating a variable
Every variable in Lua is required to wear an ID badge that identifies it. We give the variable this ID badge when we create the variable.
Let’s create a variable to store our name. We are going to give this variable the identifier name. So, let’s just type in name into the REPL.
> name
nil
Boom. We created a variable!!!
Creating multiple variables together
You can create multiple variables at the same time. Just add a comma in between each variable identifier that you want to create.
Let’s say that I wanted to store my age along with my name, I can create two variables to store that data and give them identifiers name and age.
> name, age
nil nil
Assigning value to a variable
You can assign a value to the variable by using the = operator. We also call this the assignment operator. I am going to assign my name to the name variable.
> name = "Adhithya"
Quick Side Note: If you create a variable and do not assign anything, Lua REPL will assign nil as the value to that variable. This is what happened when we create the variable name originally and that’s why Lua REPL printed out nil in response.
> name
nil
Let’s understand nil a little bit better.
Nil value
nil is a special value in Lua that indicates nothing. If you assign nil to some variable, it means that you are telling Lua that you are storing nothing in that variable.
Assigning values to multiple variables
The assignment operator can be used to assign values to multiple variables at the same time.
> name, age = "Adhithya", 30
Change value stored in the variable
Lua allows us to change the contents stored inside variables anytime. We in the computer science biz like to that property mutability of variables. Let’s say that I accidentally misspell my name (which happens more often than I care to admit). Lua allows me to fix it by using the = or assignment operator again and providing the correct spelling as the value.
> name = "Aditya" -- wrong spelling
> name = "Adhithya" -- correct spelling
Type of contents stored in variables
Lue doesn’t require you to specify the type of the content to be stored in variable ahead of time. Lua automatically figures out the type of the content you have stored in the variable. If you run the type command, Lua will tell you the type it figured out.
> type(name)
string
Since types of the content stored in variables are not set in stone in Lua, you can change the type of the content stored in the variable after the variable was created. For example: You can assign a number to the variable name and Lua will not blink an eye. If you came from a statically typed language like C++, your head might be exploding right now haha.
> name = 2
> name
2
But, please don’t do what I did above. It makes it impossible to read the code.
Allowed variable identifiers
Lua requires the variable identifiers to meet certain rules
- Variable identifiers should start with a letter or underscore.
- Variable identifiers cannot contain spaces.
- Lua has a set of reserved keywords. Lua doesn’t allow those reserved keywords to be used as a variable identifier. You can find a list of all the reserved keywords at https://www.lua.org/manual/5.1/manual.html
Here are some variable identifiers that are allowed
age = 5
start_counter = 10
_name = "Adhithya"
_1stChoice = true
Here are some variable identifiers that are not allowed
1stChoice = true
-- Problem: A variable identifier cannot start with a letter
and = 2
-- Problem: and is a reserved keyword. It cannot be used as a variable identifer
Leave a comment