lua-table

How to create nested Lua tables using the C API

房东的猫 提交于 2019-11-28 18:03:13
I want to create a table like myTable = { [0] = { ["a"] = 4, ["b"] = 2 }, [1] = { ["a"] = 13, ["b"] = 37 } } using the C API? My current approach is lua_createtable(L, 0, 2); int c = lua_gettop(L); lua_pushstring(L, "a"); lua_pushnumber(L, 4); lua_settable(L, c); lua_pushstring(L, "b"); lua_pushnumber(L, 2); lua_settable(L, c); to create the inner tables in a loop. Before, this loop, I use lua_createtable(L, 2, 0); int outertable = lua_gettop(L); to create the outer table for 2 numeric slots. But how can I save the inner tables to the outer table? Here's a full and minimal program

Why do Lua arrays(tables) start at 1 instead of 0?

雨燕双飞 提交于 2019-11-28 15:54:12
问题 I don't understand the rationale behind the decision of this part of Lua. Why does indexing start at 1? I have read (as many others did) this great paper. It seems to me a strange corner of a language that is very pleasant to learn and program. Don't get me wrong, Lua is just great but there has to be an explanation somewhere. Most of what I found (on the web) is just saying the index starts at 1. Full stop. It would be very interesting to read what its designers said about the subject. Note

How to check if a table contains an element in Lua?

人走茶凉 提交于 2019-11-28 15:27:07
问题 Is there a method for checking if a table contains a value ? I have my own (naive) function, but I was wondering if something "official" exists for that ? Or something more efficient... function table.contains(table, element) for _, value in pairs(table) do if value == element then return true end end return false end By the way, the main reason I'm using this functions is to use tables as sets, ie with no duplicate elements. Is there something else I could use ? 回答1: You can put the values

table inside table in Lua

試著忘記壹切 提交于 2019-11-28 07:23:07
问题 How can I get the data which is a table inside a table, i mean like this: t = { {a, b, c}, {d, e, f} }; if I write this line of code: print( t[1] ) the result will be —–>>> {a, b, c} BUT how can I print just the letter “a”? without using ipairs I mean is there any way to use something like t[1] ? 回答1: Have you tried t[1][1] ? That should get you the first index in the table you get from t[1] 来源: https://stackoverflow.com/questions/6807969/table-inside-table-in-lua

Search for an item in a Lua list

*爱你&永不变心* 提交于 2019-11-27 17:49:05
If I have a list of items like this: local items = { "apple", "orange", "pear", "banana" } how do I check if "orange" is in this list? In Python I could do: if "orange" in items: # do something Is there an equivalent in Lua? Jon Ericson You could use something like a set from Programming in Lua : function Set (list) local set = {} for _, l in ipairs(list) do set[l] = true end return set end Then you could put your list in the Set and test for membership: local items = Set { "apple", "orange", "pear", "banana" } if items["orange"] then -- do something end Or you could iterate over the list

Sort a Table[] in Lua

血红的双手。 提交于 2019-11-27 11:48:18
I have a Lua table that I am trying to sort. The table's format is as follows: tableOfKills[PlayerName] = NumberOfKills Which means, for example, if I had a player named Robin with a total of 8 kills and another named Jon with a total of 10 kills, the table would be: tableOfKills[Robin] = 8 tableOfKills[Jon] = 10 How would I sort that type of table to show the highest kills first? Thanks in advance! A table in Lua is a set of key-value mappings with unique keys. The pairs are stored in arbitrary order and therefore the table is not sorted in any way. What you can do is iterate over the table

Iterating through a Lua table from C++?

匆匆过客 提交于 2019-11-27 11:29:52
I'm trying to load tables from Lua to C++ but I'm having trouble getting it right. I'm getting through the first iteration just fine but then at the second call to lua_next it crashes. Any ideas? Lua file: level = { 1, 2, 3, } C++ file - First I did this: lua_getglobal( L, "level" ); for( lua_pushnil( L ); lua_next( L, 1 ); lua_pop( L, -2 ) ) { if( lua_isnumber( L, -1 ) ) { int i = (int)lua_tonumber( L, -1 ); //use number } } lua_pop( L, 1 ); Then I tried from the reference manual : lua_getglobal( L, "level" ); int t = 1; lua_pushnil( L ); while( lua_next( L, t ) ) { printf( "%s - %s", lua

How to create nested Lua tables using the C API

北慕城南 提交于 2019-11-27 11:00:33
问题 I want to create a table like myTable = { [0] = { ["a"] = 4, ["b"] = 2 }, [1] = { ["a"] = 13, ["b"] = 37 } } using the C API? My current approach is lua_createtable(L, 0, 2); int c = lua_gettop(L); lua_pushstring(L, "a"); lua_pushnumber(L, 4); lua_settable(L, c); lua_pushstring(L, "b"); lua_pushnumber(L, 2); lua_settable(L, c); to create the inner tables in a loop. Before, this loop, I use lua_createtable(L, 2, 0); int outertable = lua_gettop(L); to create the outer table for 2 numeric slots.

Search for an item in a Lua list

心已入冬 提交于 2019-11-27 04:13:42
问题 If I have a list of items like this: local items = { "apple", "orange", "pear", "banana" } how do I check if "orange" is in this list? In Python I could do: if "orange" in items: # do something Is there an equivalent in Lua? 回答1: You could use something like a set from Programming in Lua: function Set (list) local set = {} for _, l in ipairs(list) do set[l] = true end return set end Then you could put your list in the Set and test for membership: local items = Set { "apple", "orange", "pear",

lua: iterate through all pairs in table

▼魔方 西西 提交于 2019-11-27 03:49:14
问题 I have a sparse lua table and I need to iterate over it. The Problem is, it seems that lua begins the iteration at 1, and terminates as soon as it finds a nil value. Here's and example: > tab={} > tab[2]='b' > tab[5]='e' > for i,v in ipairs(tab) do print(i,v) end > --nothing is output here > tab[1]='a' > for i,v in ipairs(tab) do print(i,v) end 1 a 2 b > --terminates after 2 (first nil value is tab[3]) Is there any way to get the desired output: 1 a 2 b 5 e 回答1: You must use pairs instead of