检查表中的索引是否存在 [英] Check if index in table exist

查看:253
本文介绍了检查表中的索引是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题;我必须在表格中检查一个字段。

I have problem; I must check in my program one field in table.

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

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

and when this index exist everything is ok, but when it isn't exist, I get an error :

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

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

And it is understandable. How check if that index exist?

推荐答案

试试这个

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

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

This code will check if each table is set.

如果你确定启动args.androidIntent.extras总是设置你可以这样做

If you're sure launch args.androidIntent.extras is always set you can just do this

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

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

Fixed some mistake in the function.

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

Updated the script after the author fixed the Notification typo.

这篇关于检查表中的索引是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆