Indexing 检查表中是否存在索引

Indexing 检查表中是否存在索引,indexing,lua,lua-table,Indexing,Lua,Lua Table,我有问题;我必须在表中的一个字段中签入我的程序 if(launchArgs.androidIntent.extras.notification.custom.test_field ~= nil)then... 当这个索引存在时,一切正常,但当它不存在时,我得到一个错误: Attempt to index field 'notification' (a nil value). 这是可以理解的。如何检查该索引是否存在?试试这个 if (launchArgs and launchArgs.andr

我有问题;我必须在表中的一个字段中签入我的程序

if(launchArgs.androidIntent.extras.notification.custom.test_field ~= nil)then...
当这个索引存在时,一切正常,但当它不存在时,我得到一个错误:

Attempt to index field 'notification' (a nil value).
这是可以理解的。如何检查该索引是否存在?

试试这个

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
-- do you stuff
end
此代码将检查是否设置了每个表

如果您确定总是设置启动args.androidient.extras,您可以这样做

if(launchArgs.androidIntent.extras.notification and launchArgs.androidIntent.extras.notification.custom and launchArgs.androidIntent.extras.notification.custom.test_field)then
    -- do your stuff
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 ...))
-- checks if these two indexes were set any where in the launchArgs table and checks their type
if (IndexScan(launchArgs,"notification",false,"table") and IndexScan(launchArgs,"test_field",false,"string")) then
    -- do your stuff
end
编辑: 修正了函数中的一些错误

编辑: 在作者修复通知打字错误后更新了脚本。

请尝试以下操作:

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"))
这假定顶级变量是全局变量。根据需要进行调整。

您可以使用:

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')) -- prints 1
print(try(tbl, 'a.b.c.x')) -- prints nil
print(try(tbl, 'a.x.c.d')) -- prints nil

notifiaction
是否是
notification
的打字错误?@EtanReisner,这似乎是重点。是的,这是一个打字错误,我更正了它。根据您的打字错误修正更新了我的答案(也修复了打字错误)。这对LUA<5.2不起作用,对于希望使用它的人,请使用
getfenv(0)
,而不是
\u ENV
@user812331,Lua 5.2已经推出3年了。。。但是感谢5.1的后端口。根本不需要创建全局函数来解决这个问题;如果(。但是,我的前两个选择和你的选择,仍然需要一些时间。该功能非常适合于此。