一.数学库
print(os.time())
math.randomseed(os.time())
print(math.random(1, 100))
print(math.ceil(3.1))
print(math.floor(3.1))
print(10 % 3)
print(10 / 3)
print(2 * 2)
print(2 ^ 3)
一.数学库
print(os.time())
math.randomseed(os.time())
print(math.random(1, 100))
print(math.ceil(3.1))
print(math.floor(3.1))
print(10 % 3)
print(10 / 3)
print(2 * 2)
print(2 ^ 3)
二.字符串操作
local str = "Hello World Hello World"
print(string.upper(str))
print(string.lower(str))
print(string.len(str))
print(#str)
print(str.sub(str, 3, 5))
print(str.find(str, "tx", 0, 20))
print(str.format("PI: %.3f", math.pi) )
print(str.format("My age is %d", 22))
print(str.format("\n today is %s", os.date()))
print(str.format("\n unsigned number is %u", 888))
print(str.format("\nthe content1 is %q", "content2 is haha\n \0 content2 second line"))
print(str.rep("*", 50))
local s ="hello world from Lua"
for w in string.gmatch(s, "%a+") do
print(w)
end
print(str.reverse(str))
三.正则表达式
Pattern Item:
A pattern item can be
a single character class, which matches any single character in the class;
a single character class followed by '*', which matches 0 or more repetitions of characters in the class. These repetition items will always match the longest possible sequence;
a single character class followed by '+', which matches 1 or more repetitions of characters in the class. These repetition items will always match the longest possible sequence;
a single character class followed by '-', which also matches 0 or more repetitions of characters in the class. Unlike '*', these repetition items will always match the shortest possible sequence;
a single character class followed by '?', which matches 0 or 1 occurrence of a character in the class;
%n, for n between 1 and 9; such item matches a substring equal to the n-th captured string (see below);
%bxy, where x and y are two distinct characters; such item matches strings that start with x, end with y, and where the x and y are balanced. This means that, if one reads the string from left to right, counting +1 for an x and -1 for a y, the ending y is the first y where the count reaches 0. For instance, the item %b() matches expressions with balanced parentheses.
z = 15
function test()
local x = 10
_G.Y = 20
y = 18
print(z)
end
test()
print(x)
print(_G.Y)
print(y)
function add10(num1)
local outcome = num1 + 10
return num1, outcome
end
local _, outcome = add10(5)
print(outcome)
function counter(number, end_num)
local count = number + 1
if count < end_num then
return counter(count, end_num)
end
return count
end
print(counter(10, 15))
function counter()
local count = 0
return function ()
count = count + 1
return count
end
end
local f = counter()
print(f())
f = nil
f = counter()
print(f())
print(f())
local function sum(...)
local sums = 0
for k, v in pairs({...}) do
sums = sums + v
end
return sums
end
print(sum(1, 2, 3, 4, 5))