Check if an index exists on a table

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

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

      

and when this index exists, everything is fine, but when it does not exist I get the error:

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

      

And this is understandable. How can I check if this index exists?

+3


source to share


3 answers


try it

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 checks if each table is installed.

If you are sure that the launch args.androidIntent.extras is always set, you can simply 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

      



OR Just use this function which I posted in another answer (helps here too)

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

      

EDIT: Fixed some bug in the function.

EDIT: Updated script after the author fixed the Notification typo.

+5


source


Try also:

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

      



This assumes that the top-level variable is a global variable. Adapt if necessary.

+1


source


You can use this:

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

      

Using:

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


source







All Articles