lua笔记


Table of Contents

lua分割字符串

local ngx_find    = ngx.re.find

local function split(str, sep , count)
    local t = {}
    count = count or -1

    local nfield, nstart = 1, 1
    local nfirst,nlast = string.find(str, sep)
    -- local nfirst,nlast = ngx_find(str, sep, "jo")
    while nfirst and count ~= 0 do
        t[nfield] = string.sub(str, nstart, nfirst - 1)
        nfield = nfield+1
        nstart = nlast+1
        nfirst,nlast = string …

lua中匪夷所思的table长度


s = { 1, 2, 3, 4, 5, 6 }
print(#s)     -- output: 6
s[4] = nil
print(#s)     -- output: 6
s[7] = nil
print(#s)     -- output: 3
s[4] = 4
print(#s)     -- output: 6
s[4] = nil
print(#s)     -- output: 3

print()

s = { "1", "2", "3", "4", "5", "6" }
print(#s)     -- output: 6
s["4"] = nil
print(#s)     -- output: 6
s["7"] = nil
print(#s)     -- output …