lua-table

Three dimensional table in Lua

孤者浪人 提交于 2019-12-11 03:54:41
问题 I need the best way to store a three dimensional table for pixels. What I need to do is have multiple x,y tables (basically three dimensional) it is to raster multiple two dimensional pixel maps with transparency. You see I can create two dimensions easily like so: pixels = {{},{}} pixels[1][5] = "green" --just an example print(pixels[1][5]) However, I cannot do this like I can in Java... pixels = {{}, {}, {}} pixels[1][4][3] = "red" -- [x][y][z] print(pixels[1][4][3]) This is the

Lua, Tables: merge values of duplicate keys and remove duplicates

强颜欢笑 提交于 2019-12-11 03:25:26
问题 I've started fiddling a lot with lua recently, but I can't for my life figure this out. Let's say I have a string that looks like this: s = "a=x a=y b=z a=x" I want to remove all duplicates and merge the values of duplicate keys into a table, so that I get: t = { a = {x,y}, b = {z}, } I've been pondering about this for way too long. Any help is appreciated! 回答1: Try this: s="a=x a=y b=z a=x" s=s.." " t={} for k,v in s:gmatch("(.-)=(.-)%s+") do if t[k]==nil then t[k]={} end t[k][v]=true end

Random iteration to fill a table in Lua

夙愿已清 提交于 2019-12-11 03:08:31
问题 I'm attempting to fill a table of 26 values randomly. That is, I have a table called rndmalpha , and I want to randomly insert the values throughout the table. This is the code I have: rndmalpha = {} for i= 1, 26 do rndmalpha[i] = 0 end valueadded = 0 while valueadded = 0 do a = math.random(1,26) if rndmalpha[a] == 0 then rndmalpha[a] = "a" valueadded = 1 end end while valueadded = 0 do a = math.random(1,26) if rndmalpha[a] == 0 then rndmalpha[a] = "b" valueadded = 1 end end ... The code

How do we change the way print displays a table

南楼画角 提交于 2019-12-10 22:07:10
问题 Assuming I have a piece of code such as the following aTable = {aValue=1} aTable_mt = {} print(aTable) What must I do to make Lua print something like aTable current aValue = 1 as opposed to table: 0x01ab1d2 . So far I've tried setting the __tostring metamethod but that doesn't seem to be invoked by print . Is there some metamethod I've been missing or does the answer have nothing to do with metamethods? 回答1: __tostring works: aTable = {aValue=1} local mt = {__tostring = function(t) local

How to get updated value of table sent from C++ to Lua function?

巧了我就是萌 提交于 2019-12-10 21:03:22
问题 I'm trying to pass a float vector from C++ function to Lua function as a table parameter and then get the updated value of the vector after the Lua function call. Here's the simple example code. void myFunc(lua_State *L, std::vector<float> &vec) { lua_getglobal(L, "myFunc"); lua_newtable(L); for (size_t i=0; i<vec.size(); ++i) { lua_pushinteger(L, i+1); lua_pushnumber(L, vec[i]); lua_settable(L, -3); } if (lua_pcall(L, 1, 0, 0) != 0) { std::cout << "Error : Failed to call myFunc" << std::endl

Check if index in table exist

删除回忆录丶 提交于 2019-12-10 19:49:37
问题 I have problem; I must check in my program one field in table. if(launchArgs.androidIntent.extras.notification.custom.test_field ~= nil)then... and when this index exist everything is ok, but when it isn't exist, I get an error : Attempt to index field 'notification' (a nil value). And it is understandable. How check if that index exist? 回答1: Try this if (launchArgs and launchArgs.androidIntent and launchArgs.androidIntent.extras and launchArgs.androidIntent.extras.notification and launchArgs

Convert a CSV file into a table with defined keys in Lua

99封情书 提交于 2019-12-10 17:34:06
问题 I'm learning Lua to build scripts for a flight simulator. I have a CSV file that appear like this: Poti city, Poti,red,-295731.42857144,617222.85714285 Lanchhuti city, Poti,red,-299217.14285715,647851.42857142 Ozurgeti city, Poti,red,-317217.14285715,648422.85714285 Samtredia city, Poti,red,-287502.85714287,672022.85714285 Abasha city, Poti,red,-284245.71428573,661108.57142857 Each lines contain 5 fields ( city , region , coalition , coordinate-x and coordinate-y in simulator coordinates

Lua search tables using index or value

安稳与你 提交于 2019-12-10 15:55:26
问题 So if I have a table of colours: colour["red"] = 1 colour["blue"] = 4 colour["purple"] = 5 and I want to add red to blue, I can easily get the number values of red and blue, but then with the value 5, can I get it to return "purple" without scanning the whole table? 回答1: You would need a table with both hash and array part, if colour numbers are unique. For example: colour["purple"] = 5 colour[5] = "purple" You can create a little helper function that would facilitate populating the table,

Iterating a multidimensional Lua Table in C

心已入冬 提交于 2019-12-10 15:31:23
问题 I have a problem iterating a multidimensional Lua table in C. Let the Lua table be this i.e.: local MyLuaTable = { {0x04, 0x0001, 0x0001, 0x84, 0x000000}, {0x04, 0x0001, 0x0001, 0x84, 0x000010} } I tried to extend the C sample code: /* table is in the stack at index 't' */ lua_pushnil(L); /* first key */ while (lua_next(L, t) != 0) { /* uses 'key' (at index -2) and 'value' (at index -1) */ printf("%s - %s\n", lua_typename(L, lua_type(L, -2)), lua_typename(L, lua_type(L, -1))); /* removes

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

我的梦境 提交于 2019-12-10 08:50:32
问题 How do I get the highest integer in a table in Lua? 回答1: math.max(unpack({1, 2, 3, 4, 5})) 回答2: 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 回答3: loltable = {1, 2, 3, 4, 1, 2, 37, 1, 0} table.sort(loltable) print(loltable[