首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >检查表中是否存在索引

检查表中是否存在索引
EN

Stack Overflow用户
提问于 2014-12-15 16:13:15
回答 3查看 2.7K关注 0票数 3

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

代码语言:javascript
运行
复制
if(launchArgs.androidIntent.extras.notification.custom.test_field ~= nil)then...

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

代码语言:javascript
运行
复制
Attempt to index field 'notification' (a nil value).

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-12-15 16:28:24

尝尝这个

代码语言:javascript
运行
复制
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,则只需执行以下操作

代码语言:javascript
运行
复制
if(launchArgs.androidIntent.extras.notification and launchArgs.androidIntent.extras.notification.custom and launchArgs.androidIntent.extras.notification.custom.test_field)then
    -- do your stuff
end

或只使用这个函数,这是我在其他一些答案中发布的(在这里也有帮助)

代码语言:javascript
运行
复制
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

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

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

票数 5
EN

Stack Overflow用户

发布于 2014-12-15 16:49:19

也可以试试这个:

代码语言:javascript
运行
复制
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"))

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

票数 1
EN

Stack Overflow用户

发布于 2014-12-15 17:51:54

你可以用这个:

代码语言:javascript
运行
复制
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

用法:

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27488325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档