Lua’s learning notes (3) — expressions


1 mathematical operation operator

1.1 %Operator

In Lua%Although operators and operators in C language are the meaning of modulus, the mode of taking module is different.
In C language, modulus operation is to add the sign of the first operands after taking the absolute value of two operands.
In Lua, it is simply the remainder after rounding down the quotient relative negative infinity.


In C,

a1 = abs(a);
b1 = abs(b);
c = a1 % b1 = a1 - floor(a1/b1)*b1;

a % b = (a >= 0) ? c : -c;

In Lua,

a % b == a - math.floor(a/b)*b

Lua operates directly according to the definition of modulus. C does a little bit of processing for modular operation.


give an example:

In C

int a = 5 % 6;
int b = 5 % -6;
int c = -5 % 6;
int d = -5 % -6;


In Lua

a = 5 % 6
b = 5 % -6
c = -5 % 6
d = -5 % -6

x = {a,b,c,d}

for i,v in ipairs(x) do

--> 5
--> -1
--> 1
--> -5

It can be seen that only when the operands are the same, the two languages have the same modulus results. When the sign is different, the sign and the value of the modulus result are not equal.

The modular operation in Lua is summarized as follows: a% B, if a and B are the same sign, the result is the modulus of absolute value of a and B; if the sign is different, the result is the difference between the absolute value of B and the modulus of absolute value. The sign of the value after taking module is the same as that of B.

2 comparison operator

The result of the comparison operation isbooleanType, nottrueNamelyfalse

The supported operators are:

< <= ~= == > >=

I won’t support it!Operator.


about==Operation. The type of the two operands is compared first. If they are inconsistent, the result is false. There is no automatic conversion between the value and the string.

When comparing whether two objects are equal, only when they point to the same memory area, it is determined astrue。·

a = 123
b = 233
c = "123"
d = "123"
e = {1,2,3}
f = e
g = {1,2,3}

print(a == b)       --> false
Print (a = = C) -- > false -- Compare numbers and strings as different types
print(c == d)       --> true       
Print (E = = f) -- > true -- the reference points to the same object
Print (E = = g) -- > false -- although the contents are the same, they are different objects
Print (false = = Nil) - > false -- false is Boolean, nil is nil type

Convenient marking,-->Represents the result of the previous expression.


userdataAndtableCan be compared by meta methodeqMake changes.

In size comparison, the comparison of numbers and strings is consistent with C language. If it’s a different type of value, Lua tries to call the meta methodltandle

3 logical operator


Only think thatfalseAndnilFalse.

3.1 not

Reverse operationnotThe result isbooleanType. (andandorThe result is not necessarilyboolean)

B = not a -- A is nil, B is true
C = not not not a -- C is false

3.2 and

a and b, ifaFalse, returna, ifaTrue, returnb

Attention, whyaTime to returnaWhat about it? What’s the point? that is becauseaIt could befalseperhapsnilThe two values are false, but they are different.

3.3 or

a or b, ifaFalse, returnb, ifaTrue, returna。 Andandcontrary.


Tips:When logical operators are used to derive abooleanIt is unnecessary to consider who is returned after the logical operation, because the operation result of the logical operator conforms to the original logical meaning.

give an example

If (not (a > min and a < max)) then


3.4 others

andAndorFollowing the short-circuit principle, the second operands are evaluated only when needed.


a = 5
X = a or jjjj () -- although the latter function is not defined, it will not report an error because it will not be executed.

print(a)        -->5
print(x)        -->5

From the example above, we should be alert to logical operations, which may introduce errors that are not expected in time.

4 connector

Concatenates two strings (or numbers) into a new string. For other types, call the meta methodconcat

5 take length operator


For strings, the length is the number of characters in the string.

For a table, the length of the table is determined by finding the subscript n satisfying that t [n] is not nil and T [n + 1] is nil.

~~What about other types? ~ ~

5.1 examples

--String length
--String length
print(#"abc\0")                         --> 4
--Watch length
print(#{[1]=1,[2]=2,[3]=3,x=5,y=6})     --> 3
print(#{[1]=1,[2]=nil,[3]=3,x=5,y=6})   --> 1
") --> 4 --Watch length print(#{[1]=1,[2]=2,[3]=3,x=5,y=6}) --> 3 print(#{[1]=1,[2]=nil,[3]=3,x=5,y=6}) --> 1

6 priority

From low to high:

 <     >     <=    >=    ~=    ==
 +     -
 *     /     %
 not   #     - (unary)

Power operation > monocular operation > four rule operation > connector > comparison operator > and > or

7 table construction

BNF definition of table construction

tableconstructor ::= `{´ [fieldlist] `}´
fieldlist ::= field {fieldsep field} [fieldsep]
field ::= `[´ exp `]´ `=´ exp | Name `=´ exp | exp
fieldsep ::= `,´ | `;´

BNF definition referenceBrief introduction to BNF paradigm

give an example:

a = {}
b = {["price"] = 5; cost = 4; 2+5}
C = {[1] = 2 + 5, [2] = 2, 8, price = ABC ", [" cost "] = 4} -- tables constructed by B and C are equivalent

print(b["price"])   --> 5
print(b.cost)       --> 4
Print (B [1]) - > 7 -- if the key value is not given, the subscript is allocated in order, and the subscript starts from 1

print(c["price"])   --> abc
print(c.cost)       --> 4
print(c[1])         --> 8       
print(c[2])         --> 2

be careful:

  • If the key value is not given, the subscript is allocated in order, and the subscript starts from 1
  • If there are the same keys in the table, the value at the bottom is used as the value for the key

The existence of the above two items makes C in the above example C1The output value of is 8.


If there are the same keys in the table, the value at the bottom is used as the value for the key.

A = {[1] = 5, [1] = 6} -- then a [1] = 6


If the last field of the table is in the form of an expression and is a function, all the return values of that function are added to the table.

a = 1
function order()
    a = a + 1
    return 1,2,3,4

b = {order(); a; order(); }

c = {order(); a; (order());}

print(b[1])                     --> 1       
Print (B [2]) - > 2 -- the values in the table are not assigned after all expressions are evaluated at once
print(b[3])                     --> 1       
Print (B [4]) - > 2 -- multi return value function in the form of expression

Print (ා b) -- > 6 -- the length of the table is 6                 
Print (# C) -- > 3 -- the length of the table after adding brackets is 3

8 function

A function is an expression whose value is an object of type function. The function is instantiated each time it is executed.

8.1 function definition

There are three forms of implementing a function in Lua.

f = function() [block] end
local f; f = function() [block] end
a.f = function() [block] end

Lua provides syntax sugar to handle these three function definitions separately.

function f() [block] end
local function f() [block] end
function a.f() [block] end


abovelocalThe definition of function is notlocal f = function() [block] endTo avoid the following errors:

local f = function()
    print("local fun")
    if i==0 then 
        F () -- compilation error: attempt to call global 'f' (a nil value)
        i = i + 1

8.2 parameters of function

Formal parameters are initialized as local variables through arguments.

Add at the end of the parameter list...Indicates that the function can accept indefinite length parameters. If the tail is not added, the parameter list length of the function is fixed.

H (a,..., b) -- compilation error
f(1)                    --> a = 1, b = nil
f(1,2)                  --> a = 1, b = 2
f(1,2,3)                --> a = 1, b = 2

g(1,2)                  --> a = 1, b = 2, (nothing)
g(1,2,3)                --> a = 1, b = 2, (3)
g(1,f(4,5),3)           --> a = 1, b = 4, (3)
g(1,f(4,5))             --> a = 1, b = 4, (5)


There is also a way to define a function whose formal parameter is self

a.f = function (self, params) [block] end

The grammatical sugar forms are as follows:

function a:f(params) [block] end

Examples of use:

A = {name = "Tangyi Kejun"}
function a:f()
a: If you use a.f(), then  The error attempt to index local 'self' will be reported; in this case, it should be written as a.f (a)

:The function of function definition and call can be less than oneselfParameter. This form is rightmethodSimulation of

8.3 function call

The BNF syntax of function calls in Lua is as follows:

functioncall ::= prefixexp args

If the value of prefixexp is of type function, the function is called with the given parameters. Otherwise, the meta method “call” of prefixexp is called. The first parameter of call is the value of prefixexp, followed by the args parameter list (see2.8 meta table)。

The function call depends on whether it is passed in or notselfThe parameters are divided into.Calling and:Call.
Function calls can be divided intoParameter list call, table call, string call

[to be improved]

8.4 function closure

If a function accesses its external variables, it is a closure.

Because the variables inside the function are local variables, it can not be accessed by the outside world. In this case, if the outside world wants to change the value of the local variable, the closure can be used to achieve this purpose.
The specific implementation process is roughly like this: there is a sub function inside the function that can change the local variable. If the function returns this sub function, the external world can operate the local variable by using this sub function.

example:Using closure to change local variables

--Implement an iterator

function begin(i)
    local cnt = i

    Return function() -- this is an anonymous function that implements the auto increment function; it is also a closure because it accesses the external variable CNT
        cnt = cnt + 1
        return cnt

Iterator = begin (2) -- set the initial value of iterator to 2, and return an iterator function

Print (iterator ()) -- performs iteration

Tips:For more information on closures, please refer toHow does JavaScript closure work

Reference link

Brief introduction to BNF paradigm(brief introduction to BNF)
How do JavaScript closures work?——StackOverflow(introduces the concept of closure in JavaScript in detail)