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
5
votes
1 answer

C LUA API - Get table value at index

Assuming the following lua code: local FooTable={ ["FooKey"]="FooValue" } The index of "FooValue" is "FooKey". So I can access it like this without any issues (Assuming FooTable is on top of the stack.): lua_getfield(L, -1, "FooKey"); When I try…
Grapes
  • 2,473
  • 3
  • 26
  • 42
5
votes
2 answers

Lua Table Sorting 2 compares

I have gone over as many of the answers here as I could find that had titles I considered near enough to my problem to look into. I haven't seen anyone having my exact issue, so I'm asking a question I hope is just me being ignorant to a simple…
Bubba911
  • 53
  • 3
5
votes
2 answers

How to translate this PAWN example to LUA?

I have a new question for you all. I am wondering if you're able to do enumerations within Lua (I am not sure if this is the correct name for it). The best way I can explain this is if I show you an example using PAWN (if you know a C type language…
Bicentric
  • 1,263
  • 3
  • 12
  • 12
5
votes
1 answer

How to work with tables passed as an argument to a lua C function?

I'm going to implement a function with C language and which will be called by Lua script. This function should receive a lua table as the argument, so I should read the fields in the table.I try to do like below, but my function is crashing when I…
Suge
  • 2,808
  • 3
  • 48
  • 79
5
votes
1 answer

Performance for table length operator

Lua has the # operator to compute the "length" of a table being used as an array. In a language such as C, after you've computed the length of something, you typically don't compute it again. e.g. int len = strlen(string); Is this any different in…
charmlessCoin
  • 754
  • 3
  • 13
5
votes
2 answers

What algorithm does table.sort use?

I'm curious what algorithm Lua's default table.sort uses, only because it's slower than some other sorting algorithms I've come across. I'm also curious if Lua's table.sort is written in the Engine in C, or if it's in a library in Lua.
jocopa3
  • 796
  • 1
  • 10
  • 29
5
votes
2 answers

Lua hierarchy string to table

Is there a way that I can convert a hierarchy string into table form? Suppose the input is A.B.C.D ouput should be a table which traverses above input: A = {} A.B = {} A.B.C = {} A.B.C.D = {} Thanks.
Darshan Nair
  • 323
  • 1
  • 5
  • 11
5
votes
1 answer

Lua Table Memory?

This might be sort of a strange question, but curiosity got the best of me when I ended up getting a memory error after filling up a table with 14 million+ items. Is there a sort-of set memory limit for Lua tables, or is it somewhat dynamic at all?…
Stephen Leitnick
  • 120
  • 1
  • 10
5
votes
2 answers

How does `table.insert` work with custom tables in Lua

I wonder how does table.insert work in lua?! I am asking this because I have tried to use it on a custom table with __newindex metamethod but it seems not to call it. Is there a way to make my custom table functionality to work with table.insert?!…
Karim Tarabishy
  • 1,223
  • 1
  • 13
  • 25
5
votes
1 answer

What should a C function called from Lua that pushes a table return?

When writing a C function that pushes a table onto the stack as its return value to the Lua caller, what should it return in the C context? I know you are supposed to return the number of values that you are passing back to the Lua caller, but in…
Jesse Craig
  • 560
  • 5
  • 18
5
votes
4 answers

Lua table.concat

Is there a way to use the arg 2 value of table.concat to represent the current table index? eg: t = {} t[1] = "a" t[2] = "b" t[3] = "c" X = table.concat(t,"\n") desired output of table concat (X): "1 a\n2 b\n3 c\n"
Col_Blimp
  • 779
  • 2
  • 8
  • 26
5
votes
4 answers

Can lua function references be used as table keys?

A Lua newbie here. Can I store function references as keys in a Lua Table? Something similar to this: local fn = function() print('hello') end local my_table = {} my_table[fn] = 123 This does seem to work fine but I don't know if I can rely on the…
Vivek
  • 51
  • 1
  • 4
4
votes
1 answer

Lua: 'pairs' doesn't iterate over [1]

I quickly had to debug something, and wrote following function: function dumpTable(t) for i,v in pairs(t) do if type(v) == "table" then dumpTable(v) else print(i..":", v) end end end Now, for…
Ancurio
  • 1,698
  • 1
  • 17
  • 32
4
votes
2 answers

How can I shift all of the tables down after removing a table?

In this code: t = { num = '', } t[0].num = '0' t[1].num = '1' t[2].num = '2' Is there a way for me to delete t[0], then shift all of the table's values down, so that afterword it looks like this: t[0].num = '1' t[1].num = '2' Example with…
4
votes
1 answer

Object creation in lua

Which way is better? This Dog = {} function Dog:new() newObj = {sound = 'woof'} self.__index = self return setmetatable(newObj, self) end function…
Siddharth Roy
  • 43
  • 1
  • 3