lua-table

How are Lua tables handled in memory?

雨燕双飞 提交于 2019-12-06 03:55:57
问题 How does lua handle a table's growth? Is it equivalent to the ArrayList in Java? I.e. one that needs continuous memory space, and as it grows bigger than the already allocated space, the internal array is copied to another memory space. Is there a clever way to led with that? My question is, how is a table stored in the memory ? I'm not asking how to implement arrays in Lua. 回答1: (Assuming you're referring to recent versions of Lua; describing the behavior of 5.3 which should be (nearly?) the

Lua 5.1 workaround for __gc metamethod for tables

♀尐吖头ヾ 提交于 2019-12-06 00:26:59
I'm currently facing the problem that you can't use the __gc method for tables in Lua 5.1, as they are implemented in Lua 5.2. However, I want to release allocated native resources once the lua table gets collected. Is it possible to make a workaround which gives me the functionality of __gc metamethod in Lua 5.2 for Lua 5.1? In lua 5.1 the only lua values that work with __gc metamethod is userdata . Naturally any hack or workaround will have to involve userdata in someway. Normally there is no way to just create newuserdata from the lua side but there is one " hidden " undocumented function

How do I get the highest integer in a table in Lua?

巧了我就是萌 提交于 2019-12-05 19:45:36
How do I get the highest integer in a table in Lua? math.max(unpack({1, 2, 3, 4, 5})) A generic function for achieving this: function max(t, fn) if #t == 0 then return nil, nil end local key, value = 1, t[1] for i = 2, #t do if fn(value, t[i]) then key, value = i, t[i] end end return key, value end Which is used like this: print(max({1,2,3,4,1,2,37,1,0}, function(a,b) return a < b end)) --> 7 37 loltable = {1, 2, 3, 4, 1, 2, 37, 1, 0} table.sort(loltable) print(loltable[#loltable]) The other answer by ponzao is good, but to answer your question more specifically, if you just want to get the

How to read Lua table return value from C++

青春壹個敷衍的年華 提交于 2019-12-05 13:36:03
I have a Lua function that returns table (contains set of strings) the function run fine using this code: lua_pushstring (lua, "funcname"); lua_gettable (lua, LUA_GLOBALSINDEX); lua_pushstring(lua, "someparam"); lua_pcall (lua, 1, 1, 0); the function returns a table. How do I read it's contents from my C++ code? If you are asking how to traverse the resulting table, you need lua_next (the link also contains an example). As egarcia said, if lua_pcall returns 0, the table the function returned can be found on top of the stack. If the function doesn't throw any errors, then lua_pcall will: Remove

lua c read nested tables

那年仲夏 提交于 2019-12-05 09:47:01
below is the lua table i need to read from C: listen = { { port = 1234, address = "192.168.1.1", userdata = "liunx" }, { port = 1235, address = "192.168.1.2", userdata = "liunx1" }, { port = 1236, address = "192.168.1.3", userdata = "liunx2" } } below is the c code: #include <lua.h> /* Always include this when calling Lua */ #include <lauxlib.h> /* Always include this when calling Lua */ #include <lualib.h> /* Prototype for luaL_openlibs(), */ /* always include this when calling Lua */ #include <stdlib.h> /* For function exit() */ #include <stdio.h> /* For input/output */ void bail(lua_State

Lua - Execute a Function Stored in a Table

一世执手 提交于 2019-12-05 08:04:41
I was able to store functions into a table. But now I have no idea of how to invoke them. The final table will have about 100 calls, so if possible, I'd like to invoke them as if in a foreach loop. Thanks! Here is how the table was defined: game_level_hints = game_level_hints or {} game_level_hints.levels = {} game_level_hints.levels["level0"] = function() return { [on_scene("scene0")] = { talk("hint0"), talk("hint1"), talk("hint2") }, [on_scene("scene1")] = { talk("hint0"), talk("hint1"), talk("hint2") } } end Aaand the function definitions: function on_scene(sceneId) -- some code return

Popping the first element off an array

◇◆丶佛笑我妖孽 提交于 2019-12-05 04:09:01
I have an array x in Lua. I would like to set head = x[1] and rest = the rest of the array, so that rest[1] = x[2] , rest[2] = x[3] , etc. How can I do this? (note: I don't care if the original array gets mutated. In Javascript I would do head = x.shift() and x would contain the remaining elements.) head = table.remove(x, 1) "Pop" is a bit of a misnomer, as it implies a cheap operation, and removing the first element of an table requires relocating the rest of the contents--hence the name "shift" in JavaScript and some other languages. You want table.remove : local t = {1,2,3,4} local head =

How can I check if a lua table contains only sequential numeric indices?

浪尽此生 提交于 2019-12-04 11:35:12
问题 How can I write a function that determines whether it's table argument is a true array? isArray({1, 2, 4, 8, 16}) -> true isArray({1, "two", 3, 4, 5}) -> true isArray({1, [3]="two", [2]=3, 4, 5}) -> true isArray({1, dictionaryKey = "not an array", 3, 4, 5}) -> false I can't see any way of finding out if the numeric keys are the only keys. 回答1: ipairs iterates over indices 1..n, where n+1 is the first integer index with a nil value pairs iterates over all keys. if there are more keys than

Treating nils in sort function

佐手、 提交于 2019-12-04 11:12:24
I don't know how to handle nils my sort function gets. When I have this checking in it, table.sort crashes after some calls. if a == nil then return false elseif b == nil then return true end With this error: invalid order function for sorting . But according to the documentation, sort function should return false, if a goes after b. True otherwise. If I remove remove that code, it of course crashes of indexing nils. This has little or nothing to do with nil values in the table. The error message is generated if the comparison function itself is invalid. From the documentation for table.sort :

How are Lua tables handled in memory?

拟墨画扇 提交于 2019-12-04 07:20:47
How does lua handle a table's growth? Is it equivalent to the ArrayList in Java? I.e. one that needs continuous memory space, and as it grows bigger than the already allocated space, the internal array is copied to another memory space. Is there a clever way to led with that? My question is, how is a table stored in the memory ? I'm not asking how to implement arrays in Lua. (Assuming you're referring to recent versions of Lua; describing the behavior of 5.3 which should be (nearly?) the same for 5.0-5.2.) Under the hood, a table contains an array and a hash part. Both (independently) grow and