最简易结构lua数据表

print("最简易结构lua数据表")

local tbTest =
{
    colKey = {sn=1, name=2, age=3, phone=4},
    snKey = {[1001]=1,[1002]=2,[1003]=3},
    [1] = {1001,"小李",31,18087777777},
    [2] = {1002,"小叶",33,18908888888},
    [3] = {1003,"小卢",35,13699999999},
}
print(#tbTest,"表的长度")
--print(tbTest["colKey"]["name"])
--print(tbTest[1].phone)

--通过索引快速获取数据
local function g_tab(_tb, _idx, _colKey)
    local colIdx = _tb["colKey"][_colKey]
    --print(colIdx)
    if not colIdx then
        return
    end
    
    local result = _tb[_idx][colIdx]
    print(result)
    return result
end

--通过sn快速获得数据
local function g_tabSn(_tb, _sn, _colKey)
    local colIdx = _tb["colKey"][_colKey]
    --print(colIdx)
    if not colIdx then
        return
    end
    
    local snRowIdx = _tb["snKey"][_sn]
    
    local result = _tb[snRowIdx][colIdx]
    print(result)
    return result
end

g_tab(tbTest, 1, "name")
g_tab(tbTest, 1, "age")
g_tab(tbTest, 1, "phone")

g_tab(tbTest, 2, "name")
g_tab(tbTest, 2, "age")
g_tab(tbTest, 2, "phone")

g_tab(tbTest, 3, "name")
g_tab(tbTest, 3, "age")
g_tab(tbTest, 3, "phone")

print("我是分隔符 ============= ")

g_tabSn(tbTest, 1001, "name")
g_tabSn(tbTest, 1001, "age")
g_tabSn(tbTest, 1001, "phone")

g_tabSn(tbTest, 1002, "name")
g_tabSn(tbTest, 1002, "age")
g_tabSn(tbTest, 1002, "phone")

g_tabSn(tbTest, 1003, "name")
g_tabSn(tbTest, 1003, "age")
g_tabSn(tbTest, 1003, "phone")

print("完美")



输出

最简易结构lua数据表
3       表的长度
小李
31
18087777777
小叶
33
18908888888
小卢
35
13699999999
我是分隔符 =============
小李
31
18087777777
小叶
33
18908888888
小卢
35
13699999999
完美

你可能感兴趣的:(最简易结构lua数据表)