Updated April 11, 2023
Definition of Lua Variables
The Lua variable is placing the name to a memory location and store the data of the application. The Lua variable is giving the label to the storage position and helps to manipulate the storage data. The Lua variable helps to refere the memory location to contain the letters, digits, and symbols as information. The Lua variable works as a container to store the information and avoid multiple times use of single information. The Lua variable is the name to store the data with a specific name and use for entire Lua coding as a reference.
Syntax
The Lua variable has three types according to the use, scope, and priority of the variable.
1. The global variable:
The global variable syntax is below.
Variable_Name = variable value
Example: Stdnmbr = 1
Variable_Name = variable value;
Example: Stdnmbr = 1;
Variable1, Variable2 = value1, value2
Example: Std1, std2 = 1, 2
Variable1, Variable2 = value1, value2;
Example: Std1, std2 = 1, 2;
- The global variable is declaring variable name which is using entire coding.
- The global variable does not need any keyword to declare the name.
- The global variable identifies using only variable names and their values.
2. The local variable:
The local variable syntax is below.
local Variable_Name = variable value
Example: local Stdnmbr = 1
local Variable_Name = variable value;
Example: local Stdnmbr = 1;
local Variable1, Variable2 = value1, value2
Example: local Std1, std2 = 1, 2
local Variable1, Variable2 = value1, value2;
Example: local Std1, std2 = 1, 2;
- The local variable is declaring variable name inside of the method, chunk, and function.
- The variable is used the “local” keyword to declare the local variable name.
3. The table variable:
- The table variable syntax is below.
Variable_name = {}
- The table variable initializes the Lua table.
Variable_name[ index ] = “information”
- The table variable assigns the value to the index.
Variable_name = nil
- The table variable removes the reference of the table.
- The table variable is using to create a Lua table in the code.
- The variable and Lua table do not have any fixed relation for data storage.
- The variable used to create a table, assign value and remove the reference table.
- The variable contains every value but not the nil value of the table. The nil value is used to remove the table reference.
How to work variables in Lua?
Step 1:
The Lua text editor, Lua compiler, and Lua interpreter install in your computer as per the operating system and software version.
Or
If you do not have the software, then you can use Lua Online IDEs for coding and start Lua programming.
Step 2:
The Lua file creates with the .lua extension and writes a source code.
File name: Luacomment.lua
Step 3:
If the user required a global variable, then write Lua global variable syntax.
Stdnmbr = 1
- If the user required a local variable, then write Lua local variable syntax.
local Stdnmbr = 1
- If the user required a table variable, then write Lua table variable syntax.
Stdnmbr = {}
Stdnmbr[1] = “Lua”
Write the demo example of the Lua comment.
Filename:luacomment.lua
a = 11;
print("global variable: ", a)
local x = 10
print("local variable: ", x)
Examples
Let us discuss examples of Lua Variables.
Example #1
The Lua global variable with value example and output.
Code:
a1 = 11;
print("global variable: ", a1)
x1 = 12;
print("global variable: ", x1)
y1 , z1 = 13, 14
print("global variable: ", y1, z1)
b1 , c1 = 16, 11;
print("global variable: ", b1, c1)
d1, e1 =
print("global variable: ", d1, e1)
f1, g1 = 21
print("global variable: ", f1, g1)
Description:
The multiple variable name and value can use a single time to declare variables. If the value is not assigned but the equal sign is assigned to the variable name then the variable value becomes nil.
Output:
Example #2
the Lua local variable with value example and output.
Code:
local a1 = 11;
print("local variable: ", a1)
local x1 = 12;
print("local variable:: ", x1)
local y1 , z1 = 13, 14
print("local variable:: ", y1, z1)
local b1 , c1 = 16, 11;
print("local variable:: ", b1, c1)
local d1, e1 =
print("local variable:: ", d1, e1)
local f1, g1 = 21
print("local variable:: ", f1, g1)
Output:
Example #3
the Lua table variable with value example and output.
Code:
luaVariable = {}
print(" initialize table using variable", luaVariable)
luaVariable[2] = "lua"
print(" Assign value 1", luaVariable[2])
luaVariable["tut"] = "tutorial"
print(" Assign value 2", luaVariable["tut"])
luaVariable = nil
print(" remove the reference", luaVariable)
Description:
If we initialize the table using a variable then we get the memory location of the table. If we assign nil value to the variable then the reference is removed and the variable becomes nil.
Output:
Example #4
the Lua variable name with case sensitive example and output.
Code:
local _a1 = 11;
print("Lua case sensitive variable: ", a1)
print("Lua case sensitive variable: ", _a1)
print("Lua case sensitive variable: ", _A1)
local A1 = 12;
print("Lua case sensitive variable:: ", a1)
print("Lua case sensitive variable:: ", A1)
local y , z_1 = 13, 14
print("Lua case sensitive variable:: ", y, Z_1)
print("Lua case sensitive variable:: ", y, z_1)
m , s = 43, 44
print("Lua case sensitive variable:: ", S, M)
print("Lua case sensitive variable:: ", s, m)
Description:
You can see the above example with variable names. The s and S letter is different for language due to Lua is case sensitive language. The underscore symbol and uppercase and lowercase letter can use to start the variable name. The number is not allowed to start variable name but after the first letter allowed to use in the variable name.
Output:
Example #5
the Lua variable example and output.
Code:
luaVariable = {"online", "habit", "to", "learn"}
print("write table variable value using global variable")
for globlev = 1, 4 do
print(luaVariable[globlev])
end
print("write the relation between Lua local variable and Lua global variable")
glv = 12
local lv = 15
if lv < 16 then
local glv = 12
print(glv + 5)
else
print(glv);
end
print(glv);
Output:
Conclusion
The Lua variable is the key factor of the memory location and data storage in the Lua application. The Lua variable helps to initialize the data, assign, modify, and return the data or information. The Lua variable avoids the complication of the memory location and data storage.
Recommended Articles
We hope that this EDUCBA information on “Lua Variables” was beneficial to you. You can view EDUCBA’s recommended articles for more information.