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
6
votes
2 answers

How to remove a string from a table

I've been trying to find a way to remove a string from a table kind of like this: myTable = {'string1', 'string2'} table.remove(myTable, 'string1') but I haven't been able to find anyway to do it. Can someone help?
Lysus
  • 61
  • 1
  • 2
6
votes
2 answers

How to self-reference table during initialization

Is there a shorter way to do this: local thisismytable = { non = sequitur } thisismytable.whatismytable = thisismytable Any help would be appreciated. I don't want to re-create pre-existing functionality.
SideCode
  • 63
  • 3
6
votes
2 answers

How to split a Lua table containing sub-tables

How can I split a Lua table containing few sub-tables into two tables without changing the original table. e.g. split tbl = {{tbl1}, {tbl2}, {tbl3}, {tbl4}} into subtbl1 = {{tbl1}, {tbl2}}, subtbl2 = {{tbl3}, {tbl4}} while keep tbl…
mile
  • 402
  • 1
  • 6
  • 15
6
votes
1 answer

lua c read nested tables

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 =…
liunx
  • 751
  • 4
  • 13
  • 32
6
votes
2 answers

How to search Lua table values

I have a project that calls for a relational database like structure in an environment where an actual database isn't possible. The language is restricted to Lua, which is far from being my strongest language. I've got a table of tables with a…
Sisk
  • 71
  • 1
  • 3
6
votes
2 answers

Get Lua table size in C

How can I get a size of a Lua table in C? static int lstage_build_polling_table (lua_State * L) { lua_settop(L, 1); luaL_checktype(L, 1, LUA_TTABLE); lua_objlen(L,1); int len = lua_tointeger(L,1); printf("%d\n",len); …
briba
  • 2,857
  • 2
  • 31
  • 59
6
votes
1 answer

Read Lua table from C

I'm trying to pass a Lua table to my C program but I don´t know how to do it. My Lua code: local stages = {} stages[1] = stage1 stages[2] = stage2 stages[3] = stage3 lstage.buildpollingtable(stages) My C code: static int lstage_build_polling_table…
briba
  • 2,857
  • 2
  • 31
  • 59
6
votes
2 answers

subtract table from table in Lua

I am trying to subtract table from table in Lua, so the return table will be the subtraction of t1 from t2. This seems to be working but is there a more efficient way of doing so ? function array_sub(t1, t2) -- Substract Arrays from Array --…
nadigo
  • 107
  • 6
6
votes
3 answers

How to I pass a table from Lua into C++?

How would I pass a table of unknown length from Lua into a bound C++ function? I want to be able to call the Lua function like this: call_C_Func({1,1,2,3,5,8,13,21}) And copy the table contents into an array (preferably STL vector)?
GameFreak
  • 2,881
  • 7
  • 34
  • 38
6
votes
1 answer

How to get data from table in Lua

I have a table: Table = { button = {}, window = {}, label = {}, edit = {}, error = {} } How I can get keys and values of the table? I tried to get as: for key, value in ipairs(Table) do for k, v in ipairs(key) do …
owl
  • 4,201
  • 3
  • 22
  • 28
6
votes
1 answer

Lua table memory leak?

I have a memory leak issue about the usage of lua table, the code is below: function workerProc() -- a table holds some objects (userdata, the __gc is implememted correctly) local objs = {createObj(), createObj(), ...} while isWorking()…
Kery
  • 513
  • 8
  • 22
6
votes
2 answers

LuaPlus: How to make a function return a table?

I'm wondering how I you can create and register a function from the C++-side that returns a table when called from the Lua-side. I've tried a lot of things but nothing did really work. :/ (sorry for the long code) This for example won't work,…
Forivin
  • 14,780
  • 27
  • 106
  • 199
6
votes
1 answer

Lua: Table expected, got nil

So, I’m having an issue while trying to split strings into tables (players into teams). When there are two players only, it works like a charm, but when there are 3+ players, this pops up: “Init Error : transformice.lua:7: bad argument: table…
luafreak
  • 115
  • 1
  • 2
  • 5
6
votes
3 answers

How to load text file into sort of table-like variable in Lua?

I need to load file to Lua's variables. Let's say I got name address email There is space between each. I need the text file that has x-many of such lines in it to be loaded into some kind of object - or at least the one line shall be cut to array…
Skuta
  • 5,830
  • 27
  • 60
  • 68
6
votes
1 answer

Lua Insert table to table

Basic table, how they should be. But me need do it by function, how i can do that? local mainMenu = { caption = "Main Window", description = "test window", buttons = { { id = 1, value = "Info" }, { id = 2, value = "Return" }, { id = 3,…
Happy Day
  • 297
  • 1
  • 5
  • 14