expression
In Lua expressions include numeric constants, string constants, variables, unary and binary operators, function calls. You may also function definitions and non-conventional configuration tables.
Arithmetic Operators
Binary operators: + - * / ^ (addition, subtraction power)
unary operator: - (negative)
operand of these operators are real numbers.
Relational Operators
< > <= >= == ~=
The operator returns the result is false or true; == ~ = compares two values and, if the values of two different types, Lua that the two different; nil only with themselves equal. Lua by reference comparison tables, userdata, functions. That if and only if both represent equivalent to the same object.
a = {}; a.x = 1; a.y = 0
b = {}; b.x = 1; b.y = 0
c = a
a==c but a~=b
Lua figures by the conventional digital comparator sizes, comparison string in alphabetical order, but depends on the alphabetical order of the local environment.
When comparing the value of different types of pay particular attention to:
"0" == 0 -- false
2 < 15 -- true
"2" < "15" -- false (alphabetical order!)
To avoid inconsistent results, comparing the mixed numbers and strings, Lua being given, such as: 2 < "15"
Logical Operators
and or not
Logical operators considered false and nil is false (false), the other is true, 0 is true.
and and or computation results are not true and false, but the two operands and its related.
a and b -- 如果 a 为 false,则返回 a,否则返回 b
a or b -- 如果 a 为 true,则返回 a,否则返回 b
E.g:
print(4 and 5) --> 5
print(nil and 13) --> nil
print(false and 13) --> false
print(4 or 5) --> 4
print(false or 5) --> 5
A very practical tips: If x is false or nil initial value is assigned to x v
x = x or v
Equivalent to
if not x then
x = v
end
and a higher priority than or.
C language ternary operator
a ? b : c
Can be achieved in Lua:
(A and B) or C
results not always return true or false
print(not nil) --> true
print(not false) --> true
print(not 0) --> false
print(not not nil) --> false
Join operator
.. --两个点
String concatenation, if the operand is a digital, Lua digital translated into strings.
print("Hello " .. "World") --> Hello World
print(0 .. 1) --> 01
priority
Descending order:
^
not - (unary)
* /
+ -
..
< > <= >= ~= ==
and
or
In addition ^ ... and all binary operators are left connected.
a+i < b/2+1 <--> (a+i) < ((b/2)+1)
5+x^2*8 <--> 5+((x^2)*8)
a < y and y <= z <--> (a < y) and (y <= z)
-x^2 <--> -(x^2)
x^y^z <--> x^(y^z)
Table structure
The constructor is an expression to create and initialize the table. Lua table is a unique powerful stuff. The simplest constructor {}, to create an empty table. Directly initialize the array:
days = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"}
The Lua "Sunday" Initialization Days [1] (first element index 1), with "Monday" Initialization days [2] ...
print(days[4]) --> Wednesday
Constructors can use any initialization expression:
tab = {sin(1), sin(2), sin(3), sin(4),
sin(5),sin(6), sin(7), sin(8)}
If you want to initialize a table as a record can be used in this way:
a = {x=0, y=0} <--> a = {}; a.x=0; a.y=0
Create a table regardless of the manner in which we can add or remove any types of fields, constructors only affect initialization table to table.
w = {x=0, y=0, label="console"}
x = {sin(0), sin(1), sin(2)}
w[1] = "another field"
x.f = w
print(w["x"]) --> 0
print(w[1]) --> another field
print(x.f[1]) --> another field
w.x = nil -- remove field "x"
Each call to the constructor, Lua will create a new table, you can use the table to construct a list:
list = nil
for line in io.lines() do
list = {next=list, value=line}
end
This code is read from the standard input into each row, and then forming a linked list in reverse order. The following code prints the contents of the list:
l = list
while l do
print(l.value)
l = l.next
end
In the same constructor may be mixed and record list style style initialization, such as:
polyline = {color="blue", thickness=2, npoints=4,
{x=0, y=0},
{x=-10, y=0},
{x=-10, y=1},
{x=0, y=1}
}
This example also shows that we can nest constructors to represent complex data structures.
print(polyline[2].x) --> -10
There are two configurations initialized limit function above, for example, you can not use a negative index to initialize a table element string index can not be represented properly. The following describes a more general initialized, we expressed [expression] displayed will be initialized index:
opnames = {["+"] = "add", ["-"] = "sub",
["*"] = "mul", ["/"] = "div"}
i = 20; s = "-"
a = {[i+0] = s, [i+1] = s..s, [i+2] = s..s..s}
print(opnames[s]) --> sub
print(a[22]) --> ---
list-style initialization and record style initialization is a special case of this general initialization:
{x=0, y=0} <--> {["x"]=0, ["y"]=0}
{"red", "green", "blue"} <--> {[1]="red", [2]="green", [3]="blue"}
If you really want to array index starts at 0:
days = {[0]="Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
Note: not recommended array subscript starts at 0, otherwise a lot of the standard library can not be used.
After the last extension, "" it is optional, you can easily constructor's.
a = {[1]="red", [2]="green", [3]="blue",}
Constructor in field delimiter comma ( ",") may be separated by semicolons ( ';') Alternatively, we usually use a semicolon to separate the different types of table elements.
{x=10, y=45; "one", "two", "three"}