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
3 answers

Ordered lua table looping using C API

Consider the following lua table: foo = { bar1 = {a = 1, b = 2, c = "hello"}, bar2 = {a = 5, b = 2, c = "bbq"}, bar3 = {a = 76, b = 13, c = "pwnd"} } I am trying to iterate this table using the lua C API to retrieve the key names, bar1,…
Erunehtar
  • 1,583
  • 1
  • 19
  • 38
4
votes
2 answers

Get n-th element from end of list (table)

If I have a list (table): local list = {'foo', 'bar', 'baz', 'qux'} How do I get the n-th item from the end? (e.g., the last or second to last)
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
4
votes
1 answer

Lua 5.1 workaround for __gc metamethod for tables

I'm currently facing the problem that you can't use the __gc method for tables in Lua 5.1, as they are implemented in Lua 5.2. However, I want to release allocated native resources once the lua table gets collected. Is it possible to make a…
Turakar
  • 180
  • 2
  • 13
4
votes
1 answer

Delete variable by reference in Lua

I got several objects in several tables. Multiple functions alter and handover the objects to other functions. Lets say my table is this: objectTable = {obj1, obj2, obj3} otherobjTable = {objA, objB, objC, objD} And let's say these are initialized…
jawo
  • 856
  • 1
  • 8
  • 24
4
votes
1 answer

Accessing Lua subtables fields from C

I want to store model description in Lua and read it non-sequental. All data is store in incremental order device_pins = { {is_digital=true, name = "A", number = 1, on_time=15000000000, off_time=22000000000}, {is_digital=true, name = "B",…
pugnator
  • 665
  • 10
  • 27
4
votes
1 answer

table.insert -> remember key of inserted value

I'm inserting the value of an variable to a table and want to make sure the action succeeded. Therefore I want to return the value, but not by the var, but from the table. Is there a more simple way as iterating trough the table again? Some way to…
jawo
  • 856
  • 1
  • 8
  • 24
4
votes
1 answer

Lua Sort Table by Two Values?

So I have the following table: servers = {"ProtectedMethod" = {name = "ProtectedMethod", visits = 20, players = 2}, "InjecTive" = {name = "InjecTive", visits = 33, players = 1}}; How would I sort the sub-tables in the servers table, into a new…
ProtectedMethod
  • 109
  • 1
  • 4
  • 12
4
votes
2 answers

Lua - Getting values from nested tables

Okay so I have been searching everywhere for this, but nowhere has the answer. I have a nested table (an example): { { "Username", "Password", "Balance", }, { "username1", "password1", 1000000, }, { …
Dahknee
  • 591
  • 3
  • 12
  • 28
4
votes
2 answers

Accessing deeply nested table without error?

For a field inside a deeply nested table, for example, text.title.1.font. Even if you use if text.title.1.font then ... end it would result in an error like "attempt to index global 'text' (a nil value)" if any level of the table does not actually…
Dionysian
  • 1,195
  • 2
  • 13
  • 24
4
votes
9 answers

I need a tool to parse Lua tables, preferrably in Ruby or Java

I need a tool to parse Lua table expressions. If all else fails, I will eventually just code a small Lua module to convert tables to XML, but for the time being, I am interested in a Ruby library doing that, but failing that, I would accept tool in…
dimitarvp
  • 2,316
  • 2
  • 20
  • 29
4
votes
1 answer

How can I convert a table-formatted string into a table without using load() or loadstring()?

Another question (String to Table in Lua) has asked how to convert a string that is formatted as a table into a string, and the given answer was to use loadstring or load to convert the string into a chunk that is then executed. I also have a…
Suchipi
  • 732
  • 1
  • 6
  • 23
4
votes
3 answers

Lua - nils in table constructor

I have a following code: local ta = { nil, nil, nil, 1, a = 2 } local tb = { [4] = 1, a = 2 } for i = 1, #ta do print('ta['..i..']= ', ta[i]) end for i = 1, #tb do print('tb['..i..']= ', tb[i]) end And get the following output: ta[1]= …
Playermet
  • 43
  • 5
4
votes
2 answers

How to save boolean conditions and evaluate later

I need to create a structure. The structure must contain an array of "boolean conditions". Something like this: function ReturnStructure () local structure = { {A < 10}, {B == "smth"}, …
4
votes
2 answers

insert table values with string keys into Lua table

I'm relatively new to the Lua language and there's something I'm obviously missing about table structures. I'm trying to create a table of tables, with each table in the table having a key and the value being the respective table. Ok, that statement…
memory of a dream
  • 1,207
  • 3
  • 23
  • 32
4
votes
4 answers

How to shift all elements in a table?

I'm trying to think of an easy way to make all elements in a table shift up one. It is for a game I am playing, attempting to switch between all targets in a table! For example, let's say I'm surrounded by three mooks who want to kill me, so I…
Jonathan Picazo
  • 975
  • 3
  • 13
  • 23