lua-table

Concatenation of tables in Lua

点点圈 提交于 2019-11-27 02:37:43
问题 ORIGINAL POST Given that there is no built in function in Lua, I am in search of a function that allows me to append tables together. I have googled quite a bit and have tried every solutions I stumbled across but none seem to work properly. The scenario goes like this: I am using Lua embeded in an application. An internal command of the application returns a list of values in the form of a table. What I am trying to do is call that command recursively in a loop and append the returned values

How do you copy a Lua table by value?

冷暖自知 提交于 2019-11-26 22:26:28
Recently I wrote a bit of Lua code something like: local a = {} for i = 1, n do local copy = a -- alter the values in the copy end Obviously, that wasn't what I wanted to do since variables hold references to an anonymous table not the values of the table themselves in Lua. This is clearly laid out in Programming in Lua , but I'd forgotten about it. So the question is what should I write instead of copy = a to get a copy of the values in a ? To play a little readable-code-golf, here's a short version that handles the standard tricky cases: tables as keys, preserving metatables, and recursive

Lua - merge tables?

孤者浪人 提交于 2019-11-26 22:07:47
I need to merge two tables, with the contents of the second overwriting contents in the first if a given item is in both. I looked but the standard libraries don't seem to offer this. Where can I get such a function? for k,v in pairs(second_table) do first_table[k] = v end Here's what i came up with based on Doug Currie's answer: function tableMerge(t1, t2) for k,v in pairs(t2) do if type(v) == "table" then if type(t1[k] or false) == "table" then tableMerge(t1[k] or {}, t2[k] or {}) else t1[k] = v end else t1[k] = v end end return t1 end Wouldn't this work properly? function merge(t1, t2) for

Sort a Table[] in Lua

丶灬走出姿态 提交于 2019-11-26 15:46:10
问题 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! 回答1: A table in Lua is a set of key-value mappings with unique keys. The pairs are stored in

Iterating through a Lua table from C++?

六眼飞鱼酱① 提交于 2019-11-26 15:36:05
问题 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

Associatively sorting a table by value in Lua

淺唱寂寞╮ 提交于 2019-11-26 11:13:14
问题 I have a key => value table I\'d like to sort in Lua. The keys are all integers, but aren\'t consecutive (and have meaning). Lua\'s only sort function appears to be table.sort, which treats tables as simple arrays, discarding the original keys and their association with particular items. Instead, I\'d essentially like to be able to use PHP\'s asort() function. What I have: items = { [1004] = \"foo\", [1234] = \"bar\", [3188] = \"baz\", [7007] = \"quux\", } What I want after the sort operation

Lua - merge tables?

杀马特。学长 韩版系。学妹 提交于 2019-11-26 09:08:11
问题 I need to merge two tables, with the contents of the second overwriting contents in the first if a given item is in both. I looked but the standard libraries don\'t seem to offer this. Where can I get such a function? 回答1: for k,v in pairs(second_table) do first_table[k] = v end 回答2: Here's what i came up with based on Doug Currie's answer: function tableMerge(t1, t2) for k,v in pairs(t2) do if type(v) == "table" then if type(t1[k] or false) == "table" then tableMerge(t1[k] or {}, t2[k] or {}

How do you copy a Lua table by value?

为君一笑 提交于 2019-11-26 08:21:34
问题 Recently I wrote a bit of Lua code something like: local a = {} for i = 1, n do local copy = a -- alter the values in the copy end Obviously, that wasn\'t what I wanted to do since variables hold references to an anonymous table not the values of the table themselves in Lua. This is clearly laid out in Programming in Lua, but I\'d forgotten about it. So the question is what should I write instead of copy = a to get a copy of the values in a ? 回答1: To play a little readable-code-golf, here's a