lua笔记
2018年01月29日 周一
lua
coding
分割字符串
1 local ngx_find = ngx.re.find
2
3 local function split (str, sep , count)
4 local t = {}
5 count = count or - 1
6
7 local nfield, nstart = 1 , 1
8 local nfirst,nlast = string.find(str, sep)
9 -- local nfirst,nlast = ngx_find(str, sep, "jo")
10 while nfirst and count ~= 0 do
11 t[nfield] = string.sub(str, nstart, nfirst - 1 )
12 nfield = nfield+ 1
13 nstart = nlast+ 1
14 nfirst,nlast = string.find(str, sep, nstart)
15 -- nfirst,nlast = ngx_find(str, sep, "jo", nil, nstart)
16 count = count- 1
17 end
18 t[nfield] = string.sub(str, nstart)
19 return t
20 end
保留小数后n位
1 local function round (num, n)
2 local p = math.pow(10 , n or 3 )
3 local m = num * p
4 local _, d = math.modf(m)
5 if d < 0.5 then return math.floor(m) / p end
6 return math.ceil(m) / p
7 end
随机字符串
https://gist.github.com/haggen/2fd643ea9a261fea2094
上一篇
flask_maple文档
下一篇
Emacs自动更新文件头