Table of contents
Open Table of contents
Getting Started with Lua Scripts
Lua is case-sensitive.
Prerequisites
Install Lua locally. You can search Google for installation instructions.
Basics
Comments
-- This is a single-line comment.
-- [[
This is a multiline comment.
This is a multiline comment.
-- ]]
Reserved Keywords
Reserved keyword documentation
Variables
Types of Undeclared or Uninitialized Variables
The default value of both undeclared variables and variables declared without initialization is nil.
Local Variables
Declare a local variable with the local keyword, for example:
-- This is a local variable.
local a = 10
function max()
-- This is also a local variable.
local a = 10
end
Global Variables
Variables without the local keyword are global, for example:
-- This is a global variable.
a = 10
Note: even inside a function, a variable is global if it is not declared with local.
function max()
a = 10
end
Where Are Global Variables Stored?
In a table. See this explanation of _G.
What Happens If Global Variables Are Not Deleted?
So far, I have not found an explanation in the official documentation or books of the effects of leaving global variables undeleted. The book also says that users need not worry about Lua’s memory, because memory management is automatic.
How Do You Delete a Global Variable?
Assign nil to it.
a = 10
-- Delete the global variable.
a = nil
Initializing Multiple Variables
The basic syntax places variables in order on the left of the equals sign and their corresponding values in order on the right.
local a, b = 10, 20
If there are fewer values on the right than variables on the left, variables without a corresponding value receive Lua’s default value, nil.
a, b = 10
If there are more values on the right than variables on the left, the extra values are automatically ignored.
a, b = 10, 20, 30
Data Types
Use type to determine a variable’s type:
type(123)
-
nil: no value.
When comparing the type to nil, put nil in quotes.
type(X) == 'nil' -
boolean
-
number: a double-precision floating-point number.
-
string: single or double quotes.
Use # to calculate the string length.
local string str = 'message' print("String length:", #str)Another common way to concatenate strings:
local msg1 = 'msg1' print(msg1 .. 'append' .. 'builder')Note: with this syntax, the string must be a local variable.
Common string operations, including searching, case conversion, substrings, and concatenation.
-
function
Lua functions can return multiple values, separated by commas.
function max() a, b = 10, 20 return a, b end -- Call the function. max()Call the function after declaring it.
-
table
There are a few other types I have not listed because I do not currently need them at work.
Conditional Expressions
if
local a, b = 10, 10
if (a == b) then
print("if expression")
end
if else
local a = 10
if (a == c) then
else
print("if-else expression")
end
Nested if/elseif
local a, b = 10, 10
if (a == c) then
elseif (a == d) then
elseif(a ==b ) then
print("if-elseif expression")
end
Arrays
Declaring an Array
array = {'index1', 'index3'}
Loops
while Loops
a = 10
while (a < 20) do
a = a+1
if (a > 18) then
break
end
print("value of a:", a)
end
for Loops
for i = 10, 1, -1 do
print("value of i:", i)
end
Use the built-in ipairs function to iterate over an array:
local array = {'jack', 'tom', 'bob'}
for k, v in ipairs(array) do
print("Value in this iteration:", v)
print('Key in this iteration:', k)
end
repeat…until Loops
repeat is similar to do-while: the loop executes at least once, regardless of the condition.
b = 10
repeat
b = b+1
print("value of b:", b)
until(b > 15)
Error Handling
I have not actually worked with this part yet.
Calling Redis from Lua Scripts
Use redis.call. See the documentation for related redis. commands.
local rc, rl, ll, key, value = redis.call, redis.log, redis.LOG_NOTICE, KEYS[1], ARGV[1]
local result = rc("SETEX", key, 60, value)
print("result value is:", result)
Function Libraries
Quickly Looking Up Lua Built-in Functions
You can quickly look up unfamiliar functions in the library list at the bottom of the official documentation and in the Lua standard library section of the official manual.
Resources
Example Code
Example code for this article.
Documentation
IntelliJ IDEA Plugin
- EmmyLua: I recommend installing this plugin for convenient Lua syntax completion and debugging.
