lua-table

Attempt to call field 'contains' (a nil value) | How can I check the table to see if it has a server?

纵然是瞬间 提交于 2019-12-25 09:02:13
问题 What I am trying (Discord Bot) is to make a command called !say serverID channelID arg The point of this command is that I will pm the bot with this command and the bot writes the arg in the defined channelID from the server. Library (litcord) : https://github.com/satom99/litcord A part of the code (Yes, this has issues...) : local server = client.servers:getAll('id', serverID) if server then if server.contains('id', serverID) then for _, serverID in pairs(server) do if serverID == serverID

sort an array of associatives arrays in lua

心已入冬 提交于 2019-12-24 17:25:33
问题 I have an array that have this structure : [{"value":{"date":"2015-12-27T16:02:38.109Z", "read":"no"}, "key":"56800b9e9669ef7312d94f4c"}, {"value":{"date":"2015-12-30T13:01:30.580Z", "read":"no"}, "key":"5683d5aaec6a8c2428ca1011"},...] I want to sort this array by date in descending order with pagination. I tried this for example where wholeList is the name of the array and if want to return from indice 1 to 9: local function compare(a,b) return os.date(b["date"]) < os.date(a["date"]) end

Lua insert multiple variables into a table

限于喜欢 提交于 2019-12-24 14:53:06
问题 My question is how (or if) you can insert two values into a lua table. I got a function that returns (a variable number of values) function a(x, y) return x, y end and another function that inserts that points into a table, function b(x, y) table.insert(myTable, x, y) end So how can i make, that i can call function b with a variable number of arguments and insert them all into my table? 回答1: If the last parameter for your function is ... (called a vararg function), the Lua interpreter will

table value sorting in lua

我是研究僧i 提交于 2019-12-24 11:06:44
问题 I got an table like this: tbl = { ['etc1'] = 1337, ['etc2'] = 14477, ['etc3'] = 1336, ['etc4'] = 1335 } And now I need to sort this table to get output from highes to lowest value: tbl = { ['etc2'] = 14477, ['etc1'] = 1337, ['etc3'] = 1336, ['etc4'] = 1335 } Already tried lots of functions like table.sort or others from the official manual, but nothing helped. So hope you'll help me out guys! Regards. 回答1: Lua tables do not have ordering other than by their keys. You will need to structure

Split a string into individual characters and place the characters into a table

本秂侑毒 提交于 2019-12-24 05:27:06
问题 I'm trying to make a program that can take a word as an input, find each letter of the word and then run it through an encoding system I have. Each letter is assigned to a specific number. I'd like the program to be able to identify a letter and save it's number into a variable so it can be run through a few math operations. I have the math portion done and working, but it can only take one character at a time. Judging from some tutorials I've found online for the LUA tables and strings

set table random value from table

非 Y 不嫁゛ 提交于 2019-12-24 01:44:18
问题 I have the following code: wrg = { "1.png", "2.png", "3.png", "4.png" }; table = { } for i = 1, 4 do table[ i ] = wrg[ math.random( 1, #wrg ) ] end for i = 1, 4 do print( table[ i ] ) end output: 4.png 2.png 4.png 4.png I don't need repeat "4.png" how fix? 回答1: You need a random permutation. For instance, this code: wrg = { "1.png", "2.png", "3.png", "4.png" }; t = {} n=#wrg for i=1,n do t[i]=wrg[i] end math.randomseed(os.time()) for i=1,n-1 do local j=math.random(i,n) t[i],t[j]=t[j],t[i] end

lua 5.2 changes the order of elements in the table

岁酱吖の 提交于 2019-12-23 15:29:43
问题 In lua 5.1 the code: sums = { ["LD1"] = { }, ["LD2"] = { }, ["LD3"] = { }, ["LD4"] = { }, ["I1"] = { }, ["I2"] = { }, ["I3"] = { } } for fld = 1, 22, 1 do table.insert( sums["LD1"] , 0 ); table.insert( sums["LD2"] , 0 ); table.insert( sums["LD3"] , 0 ); table.insert( sums["LD4"] , 0 ); table.insert( sums["I1"] , 0 ); table.insert( sums["I2"] , 0 ); table.insert( sums["I3"] , 0 ); end for i,O in pairs(sums) do print(i) end Shows the sequence: (first execution) LD1 LD2 LD3 LD4 I1 I2 I3 (second

How to delete a table in Lua?

自作多情 提交于 2019-12-23 13:28:11
问题 Let us see the following codes. do local a = {1,2,3} function a:doSth() self = nil end a:doSth() if a then print("Still has a...") end end I found that this method doesn't work. The table a still exists. Why? I know the a = nil can reclaim the memory which table a holds. How to directly get the memory holds by the table a and free the memory just like delete in C++? 回答1: function a:doSth() self = nil end Is syntax sugar for function a.doSth(self) self = nil end Your above code, there are two

How to prefix a Lua table?

大憨熊 提交于 2019-12-23 12:51:06
问题 I have a lua file whose content is lua Table as below: A={} , A.B={} , A.B.C=0; , The problem is I want to add prefix XYZ before each above statements. So after the parse the database should have something loke this: XYZ.A={} , XYZ.A.B={} , XYZ.A.B.C={} , Any ideas? Thanks in advance 回答1: You can load the file with XYZ as is environment: loadfile("mydata","t",XYZ) . See loadfile in the manual. This works in Lua 5.2. For Lua 5.1, use loadfile followed by setfenv . 回答2: If you can afford

Lua: “dragging” sequence of elements within array

旧城冷巷雨未停 提交于 2019-12-23 12:01:26
问题 I'm trying to create a function that 'drags' a sequential number of elements to a new location within the array, constrained to the current size of the array. Other items should jiggle round the 'dragged' items. For example, if my array has 7 elements and I want to drag the middle three... 1, 2, 3, 4, 5, 6, 7 <-- keys a, b, C, D, E, f, g <-- values The uppercase chars are the ones I want to 'drag'. If I drag to start of the array (drag to 1) the array would look like this: 1, 2, 3, 4, 5, 6, 7