How to find out whether there is a specific key for a given Lua table besides traversing the whole table?

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

How to find out whether there is a specific key for a given Lua table besides traversing the whole table?

孙世龙 sunshilong
How to find out whether there is a specific key for a given Lua table?

I know that traversing the whole table could achieve this goal, but
it's not an efficient method. Is there any better method?
Reply | Threaded
Open this post in threaded view
|

Re: How to find out whether there is a specific key for a given Lua table besides traversing the whole table?

Luiz Henrique de Figueiredo
Reply | Threaded
Open this post in threaded view
|

Re: How to find out whether there is a specific key for a given Lua table besides traversing the whole table?

孙世龙 sunshilong
>Simply check if yourtable[yourkey] is nil
This is a Lua script.
How to achieve this goal(i.e the subject)  by C api?

On Thu, Jan 14, 2021 at 6:07 PM Luiz Henrique de Figueiredo
<[hidden email]> wrote:
>
> Cross-posted to
> https://stackoverflow.com/questions/65716853/how-to-find-out-whether-there-is-a-specific-key-for-a-given-lua-table-besides-tr
Reply | Threaded
Open this post in threaded view
|

Re: How to find out whether there is a specific key for a given Lua table besides traversing the whole table?

Spar
Why didn't you specify that you need C API solution?

Use lua_getfield or lua_rawget with lua_type and LUA_TNIL
On 14 Jan 2021, 14:43 +0300, 孙世龙 sunshilong <[hidden email]>, wrote:
Simply check if yourtable[yourkey] is nil
This is a Lua script.
How to achieve this goal(i.e the subject) by C api?

On Thu, Jan 14, 2021 at 6:07 PM Luiz Henrique de Figueiredo
<[hidden email]> wrote:

Cross-posted to
https://stackoverflow.com/questions/65716853/how-to-find-out-whether-there-is-a-specific-key-for-a-given-lua-table-besides-tr
Reply | Threaded
Open this post in threaded view
|

Re: How to find out whether there is a specific key for a given Lua table besides traversing the whole table?

Francisco Olarte
In reply to this post by 孙世龙 sunshilong
On Thu, Jan 14, 2021 at 12:43 PM 孙世龙 sunshilong <[hidden email]> wrote:
> >Simply check if yourtable[yourkey] is nil
> This is a Lua script.
> How to achieve this goal(i.e the subject)  by C api?

Translate to the C API, push the table, push the key, lookup using
https://www.lua.org/manual/5.4/manual.html#lua_gettable

You may need some more movements, depending on where you have your
table and key stored, i.e., you may need something like this for
globals:

lua_getglobal(L, "yourtable"); /* Table is at -1 */
lua_getglobal(L, "yourkey"); /* Table is at -2, key at -1 */
lua_gettable(L, -2); /* Table still at -2, value at -1 */

And some error checking if you deem it neccessary.

FOS
Reply | Threaded
Open this post in threaded view
|

Re: How to find out whether there is a specific key for a given Lua table besides traversing the whole table?

Spar
One more way with lua_isnil.

lua_pushstring(L, "key");
lua_rawget(L, 1); // Assuming table is at index 1
if (lua_isnil(L, -1)) {
    // It's nil
} else {
    // It's something else
}
On 14 Jan 2021, 14:55 +0300, Francisco Olarte <[hidden email]>, wrote:
On Thu, Jan 14, 2021 at 12:43 PM 孙世龙 sunshilong <[hidden email]> wrote:
Simply check if yourtable[yourkey] is nil
This is a Lua script.
How to achieve this goal(i.e the subject) by C api?

Translate to the C API, push the table, push the key, lookup using
https://www.lua.org/manual/5.4/manual.html#lua_gettable

You may need some more movements, depending on where you have your
table and key stored, i.e., you may need something like this for
globals:

lua_getglobal(L, "yourtable"); /* Table is at -1 */
lua_getglobal(L, "yourkey"); /* Table is at -2, key at -1 */
lua_gettable(L, -2); /* Table still at -2, value at -1 */

And some error checking if you deem it neccessary.

FOS
Reply | Threaded
Open this post in threaded view
|

Re: How to find out whether there is a specific key for a given Lua table besides traversing the whole table?

孙世龙 sunshilong
In reply to this post by Spar
Spar > Use lua_getfield or lua_rawget with lua_type and LUA_TNIL
Could you please give me an example to achieve this goal by using
`lua_rawgetp`.
If the key exists, it must be a string whose name is known before.

On Thu, Jan 14, 2021 at 7:54 PM Spar <[hidden email]> wrote:

>
> Why didn't you specify that you need C API solution?
>
> Use lua_getfield or lua_rawget with lua_type and LUA_TNIL
> On 14 Jan 2021, 14:43 +0300, 孙世龙 sunshilong <[hidden email]>, wrote:
>
> Simply check if yourtable[yourkey] is nil
>
> This is a Lua script.
> How to achieve this goal(i.e the subject) by C api?
>
> On Thu, Jan 14, 2021 at 6:07 PM Luiz Henrique de Figueiredo
> <[hidden email]> wrote:
>
>
> Cross-posted to
> https://stackoverflow.com/questions/65716853/how-to-find-out-whether-there-is-a-specific-key-for-a-given-lua-table-besides-tr
Reply | Threaded
Open this post in threaded view
|

Re: How to find out whether there is a specific key for a given Lua table besides traversing the whole table?

Roberto Ierusalimschy
In reply to this post by Spar
> One more way with lua_isnil.
>
> lua_pushstring(L, "key");
> lua_rawget(L, 1); // Assuming table is at index 1
> if (lua_isnil(L, -1)) {
>     // It's nil
> } else {
>     // It's something else
> }

Or else:

  lua_pushstring(L, "key");
  if (lua_rawget(L, 1) == LUA_TNIL) {  // Assuming table is at index 1
      // It's nil
  } else {
      // It's something else
  }

-- Roberto
Reply | Threaded
Open this post in threaded view
|

Re: How to find out whether there is a specific key for a given Lua table besides traversing the whole table?

孙世龙 sunshilong
>Assuming table is at index 1
Index 1?
In general, the index of the stack is a negative number, e.g -1, -2, and so on.

On Thu, Jan 14, 2021 at 10:11 PM Roberto Ierusalimschy
<[hidden email]> wrote:

>
> > One more way with lua_isnil.
> >
> > lua_pushstring(L, "key");
> > lua_rawget(L, 1); // Assuming table is at index 1
> > if (lua_isnil(L, -1)) {
> >     // It's nil
> > } else {
> >     // It's something else
> > }
>
> Or else:
>
>   lua_pushstring(L, "key");
>   if (lua_rawget(L, 1) == LUA_TNIL) {  // Assuming table is at index 1
>       // It's nil
>   } else {
>       // It's something else
>   }
>
> -- Roberto
Reply | Threaded
Open this post in threaded view
|

Re: How to find out whether there is a specific key for a given Lua table besides traversing the whole table?

Jonathan Goble
On Thu, Jan 14, 2021 at 8:10 PM 孙世龙 sunshilong <[hidden email]> wrote:
>Assuming table is at index 1
Index 1?
In general, the index of the stack is a negative number, e.g -1, -2, and so on.

Not in the common case of referring to arguments passed to your C function from Lua, which are usually referred to with positive numbers (since the first argument is always at index 1, the second argument at index 2, etc.)
Reply | Threaded
Open this post in threaded view
|

Re: How to find out whether there is a specific key for a given Lua table besides traversing the whole table?

孙世龙 sunshilong
>> >Assuming table is at index 1
>> Index 1?
>> In general, the index of the stack is a negative number, e.g -1, -2, and so on.

>Not in the common case of referring to arguments passed to your C function from Lua, which are usually referred to with positive numbers
> (since the first >argument is always at index 1, the second argument at index 2, etc.)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Could you please explain that(i.e the emphasised part) in more detail?


On Fri, Jan 15, 2021 at 9:15 AM Jonathan Goble <[hidden email]> wrote:
>
> On Thu, Jan 14, 2021 at 8:10 PM 孙世龙 sunshilong <[hidden email]> wrote:
>>
>> >Assuming table is at index 1
>> Index 1?
>> In general, the index of the stack is a negative number, e.g -1, -2, and so on.
>
>
> Not in the common case of referring to arguments passed to your C function from Lua, which are usually referred to with positive numbers (since the first argument is always at index 1, the second argument at index 2, etc.)
Reply | Threaded
Open this post in threaded view
|

Re: How to find out whether there is a specific key for a given Lua table besides traversing the whole table?

孙世龙 sunshilong
>> >Assuming table is at index 1
>> Index 1?
>> In general, the index of the stack is a negative number, e.g -1, -2, and so on.

>Not in the common case of referring to arguments passed to your C function from Lua, which are usually referred to with positive numbers
> (since the first >argument is always at index 1, the second argument at index 2, etc.)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Could you please explain that(i.e the emphasised part) in more detail?
I'd appreciate it if you could give me a simple example which could
make it clearer.c

ADDED:
I passed a negtive number to lua_rawget function and it worked indeed.

Here is the pseudocode:

myRegFunc()                                //omit types
{
       assertIfNotLuaString(-1);         //the index -1 is LUA_TSTRING indeed.
       pushLuaString("keyName");    //internally invoking lua_pushstring
      assert(lua_rawget(-2)==LUA_TNIL);  // no assertion, the key is
found indeed.
}

On Fri, Jan 15, 2021 at 9:34 AM 孙世龙 sunshilong <[hidden email]> wrote:

>
> >> >Assuming table is at index 1
> >> Index 1?
> >> In general, the index of the stack is a negative number, e.g -1, -2, and so on.
>
> >Not in the common case of referring to arguments passed to your C function from Lua, which are usually referred to with positive numbers
> > (since the first >argument is always at index 1, the second argument at index 2, etc.)
>     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> Could you please explain that(i.e the emphasised part) in more detail?
>
>
> On Fri, Jan 15, 2021 at 9:15 AM Jonathan Goble <[hidden email]> wrote:
> >
> > On Thu, Jan 14, 2021 at 8:10 PM 孙世龙 sunshilong <[hidden email]> wrote:
> >>
> >> >Assuming table is at index 1
> >> Index 1?
> >> In general, the index of the stack is a negative number, e.g -1, -2, and so on.
> >
> >
> > Not in the common case of referring to arguments passed to your C function from Lua, which are usually referred to with positive numbers (since the first argument is always at index 1, the second argument at index 2, etc.)
Reply | Threaded
Open this post in threaded view
|

Re:Re: How to find out whether there is a specific key for a given Lua table besides traversing the whole table?

suote127
English is not my native language,so it is maybe a little difficult to understand me.Sorry.
>Could you please explain that(i.e the >emphasised part) in more detail? 
Negative indexes access the stack from the top.So if there are n things on the stack,the negative index -i is equal to n-i+1
A C function that was called by Lua can only access the arguments pushed by Lua,just like the at first the stack was empty and then Lua pushed the arguments.
When we call a C function,Lua will push the arguments from left to right one by one.So the first argument passed to the C function is at index 1.

>I'd appreciate it if you could give me a simple >example which could 
>make it clearer.c 

e.g.
// C function
int foo(lua_State *state)
{
    lua_pushvalue(state,-3);
    lua_pushvalue(state,1);
    return 2;
}
It will return two same values("foo1") when we call it like
foo("foo1",13,{}); -- return "foo1"  "foo1"