Questions tagged [lua-table]

This tag refers to the table type in Lua that implements associative arrays.

The table type implements associative arrays. An associative array is an array that can be indexed not only with numbers, but also with strings or any other value of the language, except nil. Moreover, tables have no fixed size; you can add as many elements as you want to a table dynamically.

Tables in Lua are neither values nor variables; they are objects.

Creating table

t = {5, 10, 15, apple="red", banana="yellow" }

Short note about keys

Lua stores all elements in tables generically as key-value pairs. Lua does not differentiate between arrays and dictionaries. All Lua tables are actually dictionaries.

Note that keys are references to objects so you must use the same reference to get the same key into the table.

Useful Link

1403 questions
4
votes
1 answer

sorting a table in descending order in Lua

I can not get it work: tbl = { [1] = { ['etc2'] = 14477 }, [2] = { ['etc1'] = 1337 }, [3] = { ['etc3'] = 1336 }, [4] = { ['etc4'] = 1335 } } for i = 1, #tbl do table.sort(tbl, function(a, b) return a[i] > b[i] end) …
Lucas
  • 3,517
  • 13
  • 46
  • 75
4
votes
3 answers

How do I use lua keyword as a table key?

Problem When I use do, a lua keyword as a table's key it gives following error > table.newKey = { do = 'test' } stdin:1: unexpected symbol near 'do' > I need to use do as key. What should I do ?
MrAdityaAlok
  • 95
  • 1
  • 6
4
votes
1 answer

How to check a lua-table if a key is present?

Every minute I retrieve the following data from a webshop via a request. { ['action'] = 'all', ['orders'] = { ['order'] = { [1] = { ['locationId'] = 1, ['id'] = 93, …
Mark Munsterman
  • 169
  • 1
  • 3
  • 11
4
votes
2 answers

How to sort a table by its values in Lua?

I have a table consisting of key/value pairs: mytable[a] = 1, mytable[b] = 4, mytable[r] = 7, mytable[f] = 2, etc. I want to sort the table by the numbers. So I'd like the table to be {(a, 1), (f, 2), (b, 4), (r, 7)} I've tried using…
Jake
  • 97
  • 1
  • 6
4
votes
1 answer

Remove specific entry from Lua table

I am inserting into a table like this Admin = {} table.insert(Admins, {id = playerId, Count = 0}) And that works fine. How do I remove that specific admin from that table now? The following does not work, and Im sure its because ID is stored in an…
KevinM1990112qwq
  • 715
  • 2
  • 10
  • 23
4
votes
1 answer

Why would a lua module set its table's __index as itself?

I noticed a lua module setting the returned table's __index as itself local M = { _VERSION = "1.0.0" } M.__index = M function M.do() end return M What does setting a table's __index as itself accomplish? Later, you would use the module local m…
joeforker
  • 40,459
  • 37
  • 151
  • 246
4
votes
3 answers

How to get value from the table in Lua

I have table with with multiple values and I want to print each of them. To be like: 'value_1' 'value_2' etc.. table = { {'value_1'}, {'value_2'}, {'value_3'}, {'value_4'}, } I tried with for k, v but I…
whiteblXK
  • 109
  • 2
  • 3
  • 10
4
votes
2 answers

Read only iterable table in lua?

I want to have a read only table in my Lua program. If ever a key is removed or a key is associated with a new value, an error must be thrown. function readonly(table) local meta = { } -- metatable for proxy local proxy = { } -- this table…
Hatefiend
  • 3,416
  • 6
  • 33
  • 74
4
votes
1 answer

Is this a bug in lua, or a feature unknown to me?

I'm trying to teach a friend of mine how to lua, basically, and he comes to me with this code, and it stumps my head: a = {name = "aaa"} function a:new() self.x = "sss" o = {} setmetatable(o, self) self.__index = self return…
Shadowjonathan
  • 253
  • 2
  • 11
4
votes
1 answer

Pandoc Lua Filters: pairs does not show all keys in table

Since Pandoc version 2.0, there has been the ability to write Lua Filters. However, in Pandoc 2.0, I find that using Lua's pairs on an element table does not show all keys in the table. Here is a minimal example to illustrate the point. In…
IssaRice
  • 325
  • 1
  • 12
4
votes
1 answer

Iterating a multidimensional Lua Table in C

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…
4
votes
1 answer

Array as value in a lua table

I couldn't find an answer to my question: Can you have an array as value in a lua table? local colors = {"blue" = {0,0,1,1}, "green" = {0,1,0,1}, "red" = {1,0,0,1} , "orange" = {0.5, 0, 0.5, 1}, "black" = {0,0,0,1}, "gold" = {1, 215/255, 0, 1}} I…
Cing
  • 806
  • 1
  • 11
  • 29
4
votes
1 answer

Lua table.insert does not accept a string parameter

Continuing to learn Lua. I have wrote a function that removes the first sentence from each line and returns the result as a table of modified lines, where the first sentence was removed. Strangely, table.insert behaves weird in such function.…
minerals
  • 6,090
  • 17
  • 62
  • 107
4
votes
0 answers

How to pass struct from Lua to Pascal

I have Lua tables that have to pass data to Pascal. Below is the method that I'm currently using. Lua Code: local ffi=require("ffi") ffi.cdef[[ void __cdecl Print(char * S); void __cdecl Func1(struct structLIST *struct1); #pragma pack(1) typedef…
user2032083
  • 313
  • 2
  • 4
  • 14
4
votes
2 answers

How to read Lua table return value from C++

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…
nir
  • 161
  • 2
  • 7