lua-table

Lua Control Statments having odd behavior

一世执手 提交于 2019-12-14 02:43:15
问题 I'm getting a very unexpected result from what should be basic control statement operations. I have the following, a file being read with this sort of data: 1, 51, one , ab 1, 74, two , ab 0, 74, tree , ab 0, 74, for , ab 0, 74, five , ab My snip of Lua code that processes it: if file then for line in file:lines() do LineArray = line CanClaimInfo[LineArray] = {} lineData = utils.split(line,",") if lineData[1] == "0" then lineData[1] = "CAN A" elseif lineData[1] == "1" then lineData[1] = "CAN

How to get multiple returned tables from Lua function?

二次信任 提交于 2019-12-13 18:12:54
问题 I'm trying to send multiple float arrays from C++ to Lua function as arguments and then return multiple tables from the function so I can use them again as float arrays in C++. So my Lua function will look like this. function perform(arg1, arg2) local ret1, ret2 = {}, {} for i=1, #arg1 do ret1[i] = arg1[i] * 0.2; ret2[i] = arg2[i] * 0.3; end return ret1, ret2 end And this is how I send and return multiple tables to/from Lua function in C++. lua_getglobal(L, "perform"); for (int i=0; i<numArgs

Lua table.concat

不羁岁月 提交于 2019-12-13 14:25:50
问题 Is there a way to use the arg 2 value of table.concat to represent the current table index? eg: t = {} t[1] = "a" t[2] = "b" t[3] = "c" X = table.concat(t,"\n") desired output of table concat (X): "1 a\n2 b\n3 c\n" 回答1: Simple answer : no. table.concat is something really basic, and really fast. So you should do it in a loop anyhow. If you want to avoid excessive string concatenation you can do: function concatIndexed(tab,template) template = template or '%d %s\n' local tt = {} for k,v in

Getting one value from a table and assigning it to another with no repeats

♀尐吖头ヾ 提交于 2019-12-12 21:09:49
问题 Specifically, this is for Garry's Mod, however I don't think that matters too much in this problem. What I want to do is get one player, and set their value to another random player (so every player has a random 'target'). I want to do this with no repeats, and so that a player is not assigned to their self. To better illustrate: The only difference to that picture is that I want each player to be assigned to another random player, so more like player1 => player 5, player 3 => player 2, etc.

lua c read nested tables

北慕城南 提交于 2019-12-12 09:39:08
问题 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 */

Lua - convert string to table

China☆狼群 提交于 2019-12-12 07:33:29
问题 I want to convert string text to table and this text must be divided on characters. Every character must be in separate value of table, for example: a="text" --converting string (a) to table (b) --show table (b) b={'t','e','x','t'} 回答1: You could use string.gsub function t={} str="text" str:gsub(".",function(c) table.insert(t,c) end) 回答2: Just index each symbol and put it at same position in table. local str = "text" local t = {} for i = 1, #str do t[i] = str:sub(i, i) end 回答3: The builtin

dynamic tables or arrays

橙三吉。 提交于 2019-12-11 14:05:49
问题 I have a function that is meant to dynamically change the size of a 3D Array or table but it keeps breaking saying that it return value nil because it is out of bounds. Here is the code for it: function resize() temp = { } for h=1, height do table.insert( temp , { } ) for y=1, length do table.insert ( temp[h], { } ) for x=1, width do num = 16 if #blocks <= height then if #blocks[h] <= length then if #blocks[h][y] <= width then num = blocks[h][y][x] end end end table.insert( temp[h][y] , num )

Table with methods, how to handle/inspect them?

可紊 提交于 2019-12-11 12:09:28
问题 So without the context, I have a method that returns a table. Trying to print it out, it seems that the table mainly contains methods to be called. However not being very knowledgeable in LUA, i have no idea how to properly get some information about these methods i should call. I tried to get documentation from the creator of the thing, but there is none as far as i know. And since this is inside computercraft (minecraft mod) i don't have a lot of features to rely on either. So knowing only

Returning A Sorted List's Index in Lua

微笑、不失礼 提交于 2019-12-11 11:13:04
问题 I access object properties with an index number object = {} object.y = {60,20,40} object.g = {box1,box2,box3} -- graphic object.c = {false,false,false} -- collision -- object.y[2] is 20 and its graphic is box2 -- sorted by y location, index should be, object.sort = {2,3,1} I know table.sort sorts a list, but how can I sort the y list that returns index for the purpose of drawing each object in-front depending on the y location. Maybe the quicksort function can be edited, I don't understand it

Is there a way to iterate through lua dictionary in order that it's created? [duplicate]

瘦欲@ 提交于 2019-12-11 08:34:27
问题 This question already has answers here : How to iterate through a table in its exact order? (4 answers) Closed 4 years ago . I need to iterate through Lua dictionary in the order that it's created. For example: t = { ['some'] = 'xxx', ['random'] = 'xxx', ['data'] = 'xxx', ['in'] = 'xxx', ['table'] = 'xxx', } Normal iterating with pairs gives a random sequence order: for key, val in pairs(t) do print(key.." : "..val) end random : xxx some : xxx data : xxx table : xxx in : xxx I need: some :