How to check if table index exist

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to check if table index exist

Aidar Kamalov
Hello,

I have a table generated by luasoap.
function trackNum(tracknum)
    local ns, meth, ent = client.call {
        url = "http://mysite/api/service.asmx",
        soapaction = "http://mysite.org/Track",
        method = "Track",
        namespace = "http://mysite.org/",
        entries = {
            {
                tag = "TrackNo", tracknum,
            },
        }
    }
    local Code = ent[1][1][1][1] or "201"
    local Status = if ent[1][1][3][1][17][1] or "Unknown"
    return Code, Status
end

so, I don't know how many indexes will be in answer (btw, is it possible to address by names? for example ent['Code']) and I want to escape  error attempt to index a nil value (field '?').

What the best way to do it? something like try/except int python?

--
Aydar A. Kamalov
Reply | Threaded
Open this post in threaded view
|

Re: How to check if table index exist

Oliver Kroth
Hi,

My favourite method is to add to the result a metatable whose __index function returns an empty table with exactly that same metatable attached, which can be the same predefined table for all cases.
Effect is that always an empty table is returned if the indexes are exhausted. In your case the function  may need to check if the index is a number and return the table, else nil.

--
Oliver

Am 18.11.19 um 06:42 schrieb Aidar Kamalov:
Hello,

I have a table generated by luasoap.
function trackNum(tracknum)
    local ns, meth, ent = client.call {
        url = "http://mysite/api/service.asmx",
        soapaction = "http://mysite.org/Track",
        method = "Track",
        namespace = "http://mysite.org/",
        entries = {
            {
                tag = "TrackNo", tracknum,
            },
        }
    }
    local Code = ent[1][1][1][1] or "201"
    local Status = if ent[1][1][3][1][17][1] or "Unknown"
    return Code, Status
end

so, I don't know how many indexes will be in answer (btw, is it possible to address by names? for example ent['Code']) and I want to escape  error attempt to index a nil value (field '?').

What the best way to do it? something like try/except int python?

--
Aydar A. Kamalov

Reply | Threaded
Open this post in threaded view
|

Re: How to check if table index exist

Sergey Kovalev
In reply to this post by Aidar Kamalov
function ats(a,def)
    return function(...)
        for _,v in ipairs(table.pack(...)) do
            if type(a)~='table' then return def end
            a=a[v]
        end
        return a
    end
end

Code = ats(ent,"201")(1,1,1,1)
Status = ats(ent)(1,1,3,1,17,1) or "Unknown"

пн, 18 нояб. 2019 г. в 07:42, Aidar Kamalov <[hidden email]>:

>
> Hello,
>
> I have a table generated by luasoap.
> function trackNum(tracknum)
>     local ns, meth, ent = client.call {
>         url = "http://mysite/api/service.asmx",
>         soapaction = "http://mysite.org/Track",
>         method = "Track",
>         namespace = "http://mysite.org/",
>         entries = {
>             {
>                 tag = "TrackNo", tracknum,
>             },
>         }
>     }
>     local Code = ent[1][1][1][1] or "201"
>     local Status = if ent[1][1][3][1][17][1] or "Unknown"
>     return Code, Status
> end
>
> so, I don't know how many indexes will be in answer (btw, is it possible to address by names? for example ent['Code']) and I want to escape  error attempt to index a nil value (field '?').
>
> What the best way to do it? something like try/except int python?
>
> --
> Aydar A. Kamalov

Reply | Threaded
Open this post in threaded view
|

Re: How to check if table index exist

Aidar Kamalov
Thank you, Sergey

пн, 18 нояб. 2019 г. в 19:51, Sergey Kovalev <[hidden email]>:
function ats(a,def)
    return function(...)
        for _,v in ipairs(table.pack(...)) do
            if type(a)~='table' then return def end
            a=a[v]
        end
        return a
    end
end

Code = ats(ent,"201")(1,1,1,1)
Status = ats(ent)(1,1,3,1,17,1) or "Unknown"

пн, 18 нояб. 2019 г. в 07:42, Aidar Kamalov <[hidden email]>:
>
> Hello,
>
> I have a table generated by luasoap.
> function trackNum(tracknum)
>     local ns, meth, ent = client.call {
>         url = "http://mysite/api/service.asmx",
>         soapaction = "http://mysite.org/Track",
>         method = "Track",
>         namespace = "http://mysite.org/",
>         entries = {
>             {
>                 tag = "TrackNo", tracknum,
>             },
>         }
>     }
>     local Code = ent[1][1][1][1] or "201"
>     local Status = if ent[1][1][3][1][17][1] or "Unknown"
>     return Code, Status
> end
>
> so, I don't know how many indexes will be in answer (btw, is it possible to address by names? for example ent['Code']) and I want to escape  error attempt to index a nil value (field '?').
>
> What the best way to do it? something like try/except int python?
>
> --
> Aydar A. Kamalov



--
Aydar A. Kamalov
Reply | Threaded
Open this post in threaded view
|

回复: How to check if table index exist

最萌 小汐

When loading a large untrusted table, I would setmetatable of nil, witch saved a lot of code to checking nil.

Here is a simple implementation: https://github.com/sumneko/lua-without-check-nil/blob/master/without-check-nil.lua

 

--- sumneko

 

发件人: [hidden email]
发送时间: 20191120 13:30
收件人: [hidden email]
主题: Re: How to check if table index exist

 

Thank you, Sergey

 

пн, 18 нояб. 2019 г. в 19:51, Sergey Kovalev <[hidden email]>:

function ats(a,def)
    return function(...)
        for _,v in ipairs(table.pack(...)) do
            if type(a)~='table' then return def end
            a=a[v]
        end
        return a
    end
end

Code = ats(ent,"201")(1,1,1,1)
Status = ats(ent)(1,1,3,1,17,1) or "Unknown"

пн, 18 нояб. 2019 г. в 07:42, Aidar Kamalov <[hidden email]>:
>
> Hello,
>
> I have a table generated by luasoap.
> function trackNum(tracknum)
>     local ns, meth, ent = client.call {
>         url = "http://mysite/api/service.asmx",
>         soapaction = "http://mysite.org/Track",
>         method = "Track",
>         namespace = "http://mysite.org/",
>         entries = {
>             {
>                 tag = "TrackNo", tracknum,
>             },
>         }
>     }
>     local Code = ent[1][1][1][1] or "201"
>     local Status = if ent[1][1][3][1][17][1] or "Unknown"
>     return Code, Status
> end
>
> so, I don't know how many indexes will be in answer (btw, is it possible to address by names? for example ent['Code']) and I want to escape  error attempt to index a nil value (field '?').
>
> What the best way to do it? something like try/except int python?
>
> --
> Aydar A. Kamalov


 

--

Aydar A. Kamalov