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

Why are table literals treated differently from table references in Lua?

Here is a Lua 5.2.2 transcript, showing the declaration and indexing of a table: > mylist = {'foo', 'bar'} > print(mylist[1]) foo Why isn't the following statement legal? > print({'foo', 'bar'}[1]) stdin:1: ')' expected near '[' I can't think of…
Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101
8
votes
3 answers

Lua - Execute a Function Stored in a Table

I was able to store functions into a table. But now I have no idea of how to invoke them. The final table will have about 100 calls, so if possible, I'd like to invoke them as if in a foreach loop. Thanks! Here is how the table was…
brain56
  • 2,659
  • 9
  • 38
  • 70
8
votes
3 answers

How to create nested Lua tables using the C API

I want to create a table like myTable = { [0] = { ["a"] = 4, ["b"] = 2 }, [1] = { ["a"] = 13, ["b"] = 37 } } using the C API? My current approach is lua_createtable(L, 0, 2); int c = lua_gettop(L); lua_pushstring(L, "a"); lua_pushnumber(L,…
Etan
  • 17,014
  • 17
  • 89
  • 148
8
votes
2 answers

An Interesting phenomenon of Lua's table

I'm new to Lua, and I'm learning the usage of table these days. From tutorials I know that Lua treats numeric indexed items and non-numeric indexed items differently, so I did some tests myself, and today I found an interesting phenomenon and I…
SaltyEgg
  • 1,498
  • 1
  • 15
  • 26
7
votes
1 answer

How to notify host application when object/table are garbage collected

My host C application, that embed a Lua interpreter, needs to be notified that certain object/table in running Lua script is garbage collected, so it will do something, like record this event to log file. How can I do that?
Vertilka
  • 183
  • 2
  • 13
7
votes
2 answers

How are Lua tables handled in memory?

How does lua handle a table's growth? Is it equivalent to the ArrayList in Java? I.e. one that needs continuous memory space, and as it grows bigger than the already allocated space, the internal array is copied to another memory space. Is there a…
Johnny Willer
  • 3,717
  • 3
  • 27
  • 51
7
votes
1 answer

Iterating over table of tables with the Lua C API

I'm trying to iterate over a table of tables in Lua and output: The key of each table. The key / value pair of each entry in each table. Here is the code: void print_table(lua_State *L) { lua_pushnil(L); while(lua_next(L, -2) != 0) { …
lamma
  • 73
  • 1
  • 5
7
votes
4 answers

Is there a (simple) way to get the memory usage of a Lua table?

I'd like to find out how much memory a Lua table is using - without iterating through the table contents and counting up the usage. Is there a Lua 5.1 function or 3rd party library that could help with this.
user295625
  • 73
  • 1
  • 1
  • 3
7
votes
1 answer

ServiceStack Redis, how to return Lua table as List

I'm using the Redis client from ServiceStack. I have a Lua script that fills up a Lua table with results from several Redis calls. I want to return this table in some way. My thought was to use the method ExecLuaShaAsList from the client lib and in…
Thomas
  • 469
  • 6
  • 16
7
votes
2 answers

Lua table convert

I am new to lua, I have a table foo and I want to convert it to bar like following: foo:{key1,value2,key2,value2} ==> bar:{key1=value1,key2=value2} Does lua have a built-in method to do that ?
Yohn
  • 866
  • 8
  • 11
7
votes
3 answers

Lua: When and how to write tables to _G

I am learning Lua from a book, and I am NOT a programmer. I am trying to save a table of data to a file using the following functions (that were copied directly from the book), but the function is getting an error when trying to get a string from…
PHazer
  • 109
  • 1
  • 5
6
votes
3 answers

Nested tables and numerical keys in Lua

I'm not sure if this is possible due to the numerical indices, but hopefully someone can point me in the right direction. Given the table of: t = { 13, 200, 12, 15, 23 } how can I nest a table using the numbers? t["200"] = {"stuff", "more stuff",…
Josh
  • 3,225
  • 7
  • 30
  • 44
6
votes
1 answer

Lua: Sort String array with varying casing

I'm facing an issue with Lua while using the table.sort function. I wrote a little snippet ready for you to test, if you want to convince yourselves. test = {"apple", "Bee", "clown" } table.sort( test ) for k, v in pairs( test ) do print( k, v…
Legofan431
  • 153
  • 2
  • 11
6
votes
2 answers

Child class constructor method in Lua

Having a bit of a hard time to grasp the concept of inheritance (and metatables) in Lua. The official tutorial doesn't specifically explain how to make a constructor for the child class. The problem with my example is that player:move() is nil, so…
Mojimi
  • 2,561
  • 9
  • 52
  • 116
6
votes
1 answer

Lua script to return efficient dictionary from Redis HGETALL call

I need to use Redis HMGET from a Lua script and extract specific values in following code. But redis.call('HMGET', table_key, hkey1, hkey2, ...) return a flat array of {hkey1, val1, hkey2, val2, ...} To extract values by key I wrote: local function…
amotzg
  • 1,142
  • 12
  • 28