【问题标题】:Check if index in table exist检查表中的索引是否存在
【发布时间】:2015-02-13 19:14:25
【问题描述】:

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

if(launchArgs.androidIntent.extras.notification.custom.test_field ~= nil)then...

当这个索引存在时一切正常,但当它不存在时,我得到一个错误:

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

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

【问题讨论】:

  • notifiaction 是不是 notification 的错字?
  • @EtanReisner,这似乎是重点。
  • 是的,这是一个错字,我更正了。
  • 根据您的错字修复更新了我的答案(也修复了错字)

标签: indexing lua lua-table


【解决方案1】:

试试这个

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.androidIntent.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

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

编辑: 在作者修正了 Notification 错字后更新了脚本。

【讨论】:

  • 较短的替代方案:local T = {}; if (((((launchArgs or T).androidIntent or T).extras or T).notification or T).custom or T).test_field then end
  • @siffiejoe 该方法需要一段时间,而且不是很安全(除非在检查该字段之前总是设置 T )。但同样,我的前 2 个选项和您的选项仍然需要一些时间。该功能非常适合此。
【解决方案2】:

也试试这个:

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"))

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

【讨论】:

  • 这在 LUA getfenv(0) 而不是 _ENV
  • @user812331,Lua 5.2 已经推出 3 年了...但是感谢 5.1 的反向移植。
  • 根本不需要创建全局变量来解决问题。
【解决方案3】:

你可以用这个:

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

【讨论】:

    猜你喜欢
    • 2013-11-03
    • 2021-07-22
    • 2016-05-03
    • 2012-11-30
    • 2015-09-16
    • 2013-03-19
    • 1970-01-01
    • 2011-02-10
    • 2013-05-28
    相关资源
    最近更新 更多