检查表中的索引是否存在。

我有一个问题,我必须检查表中的一个字段在我的程序中。

if(launchArgs.androidIntent.extras.notification.custom.test_field ~= nilthen......

当这个索引存在时,一切都很好,但当它不存在时,我会得到一个错误:

Attempt to index field 'notification' (a nil value).

这是可以理解的。如何检查该索引是否存在?

点赞
用户4261756
用户4261756

尝试这样做

if (launchArgs and launchArgs.androidIntent and launchArgs.androidIntent.extras
    and launchArgs.androidIntent.extras.notification and launchArgs.androidIntent.extras.notification.custom
    and launchArgs.androidIntent.extras.notification.custom.test_field) then
-- 执行你的任务
end

此代码将检查每个表是否已设置。

如果您确信 launchArgs.androidIntent.extras 总是设置的,您可以只需执行以下操作

if(launchArgs.androidIntent.extras.notification and launchArgs.androidIntent.extras.notification.custom and launchArgs.androidIntent.extras.notification.custom.test_field)then
    -- 执行你的任务
end

或者只需使用此函数,我发布在另一个答案中(也可在此处使用)

function IndexScan(input,value,case,_type)
    if (input and type(input) == 'table') then
        if (_type) then
            if (type(value) == _type and value == input) then
                return true;
            end
        else
            if (type(value) == 'table' and value == input) then
                return true;
            end
        end
        for key,object in pairs(input) do
            if (case and type(input)=='string' and type(key)=='string') then
                if (_type) then
                    if (value:lower() == key:lower() and type(object)==_type) then
                        return true;
                    elseif(type(object)=='table') then
                        return IndexScan(object,value,case,_type)
                    end
                else
                    if (value:lower() == key:lower()) then
                        return true;
                    elseif(type(object)=='table') then
                        return IndexScan(object,value,case,_type)
                    end
                end
            else
                if (_type) then
                    if (key == value and type(object)==_type) then
                        return true
                    elseif(type(object)=='table') then
                        return IndexScan(object,value,case,_type)
                    end
                else
                    if (key == value) then
                        return true
                    elseif(type(object)=='table') then
                        return IndexScan(object,value,case,_type)
                    end
                end
            end
        end
    end
    return false;
end
-- IndexScan(@param table(table), @param index(string), @param case-sensitive(true/false), @param type (index type, string/boolean/number/table ...))
-- 检查这两个索引是否在 launchArgs 表中任何地方设置,然后检查它们的类型
if (IndexScan(launchArgs,"notification",false,"table") and IndexScan(launchArgs,"test_field",false,"string")) then
    -- 执行你的任务
end

编辑: 修复函数中的一些错误。

编辑: 在作者修复通知拼写错误后更新脚本。

2014-12-15 16:28:24
用户107090
用户107090

也可以尝试一下这种方式:

function setglobal(name,value)
    local t=_ENV
    local f="_G"
    for x in name:gmatch("[^.]+") do
        if t[f]==nil then t[f]={} end
        t=t[f]
        f=x
    end
    t[f]=value
end

function getglobal(name)
    local t=_ENV
    for x in name:gmatch("[^.]+") do
        t=t[x]
        if t==nil then return nil,x end
    end
    return t
end

setglobal("launchArgs.androidIntent.extras.notification.custom.test_field",2014)
print(getglobal("launchArgs.androidIntent.extras.notification.custom.test_field"))
print(getglobal("launchArgs.androidIntent.extras.notifiaction.custom.test_field"))

这个假设顶级变量是全局变量。根据需要进行调整。

2014-12-15 16:49:19
用户312586
用户312586

你可以使用以下代码:

local function try(root, query)
  local ids, len = {}, 0
  for id in query:gmatch("%w+") do
    len = len + 1
    ids[len]= id
  end

  local node = root
  for i=1,len do
    if type(node) ~= 'table' then return nil end
    node = node[ids[i]]
  end

  return node
end

用法:

local tbl = { a = { b = { c = { d = 1 } } } }

print(try(tbl, 'a.b.c.d')) -- 输出 1
print(try(tbl, 'a.b.c.x')) -- 输出 nil
print(try(tbl, 'a.x.c.d')) -- 输出 nil
2014-12-15 17:51:54