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

Sort a Table[] in Lua

I have a Lua table that I am trying to sort. The table's format is as follows: tableOfKills[PlayerName] = NumberOfKills Which means, for example, if I had a player named Robin with a total of 8 kills and another named Jon with a total of 10 kills,…
sgtaziz
  • 383
  • 1
  • 3
  • 6
27
votes
6 answers

how to delete all elements in a Lua table?

How do I delete all elements inside a Lua table? I don't want to do: t = {} table.insert(t, 1) t = {} -- this assigns a new pointer to t I want to retain the same pointer to t, but delete all elements within t. I tried: t = {} table.insert(t,…
bob
  • 1,941
  • 6
  • 26
  • 36
27
votes
4 answers

Lua: How to find out if an element is a table instead of a string/number?

As the title says, what function or check can I do to find out if a lua element is a table or not? local elem = {['1'] = test, ['2'] = testtwo} if (elem is table?) // <== should return true
unwise guy
  • 1,048
  • 8
  • 18
  • 27
25
votes
7 answers

Associatively sorting a table by value in Lua

I have a key => value table I'd like to sort in Lua. The keys are all integers, but aren't consecutive (and have meaning). Lua's only sort function appears to be table.sort, which treats tables as simple arrays, discarding the original keys and…
Ben Blank
  • 54,908
  • 28
  • 127
  • 156
25
votes
6 answers

How to check if two tables(objects) have the same value in Lua

I wanna check if two tables have the same value in Lua, but didn't find the way. I use the operator ==, it seems just to check the same objects, but not the elements in the table. If I have two tables, a={} b={} the value of a==b is false. but…
LetsOMG
  • 429
  • 1
  • 5
  • 11
23
votes
7 answers

Display contents of tables in lua

What I'm trying to do is display the content of table using the following code in Lua. local people = { { name = "Fred", address = "16 Long Street", phone = "123456" }, { name = "Wilma", address = "16 Long Street", phone…
Neenu
  • 479
  • 1
  • 6
  • 15
21
votes
2 answers

How to iterate Lua table from end?

How do I iterate a simple Lua table, that is a sequence, from end? Example of wanted behavior: local mytable = {'a', 'b', 'c'} for i, value in reversedipairs(mytable) do print(i .. ": " .. value) end should output 3: c 2: b 1: a How to…
Franz Wexler
  • 1,112
  • 2
  • 9
  • 15
20
votes
3 answers

Iterating through a Lua table from C++?

I'm trying to load tables from Lua to C++ but I'm having trouble getting it right. I'm getting through the first iteration just fine but then at the second call to lua_next it crashes. Any ideas? Lua file: level = { 1, 2, 3, } C++ file - First…
Jonas
  • 1,532
  • 3
  • 16
  • 20
19
votes
2 answers

Lua table library removed?

I'm trying to learn the ropes on Lua, and I was going through the online tutorials. One problem I tried to solve was to examine a table local foo = {} to see how many elements it had. The tutorial gave the suggestion to use local length =…
Piotr
  • 541
  • 4
  • 19
18
votes
3 answers

Getting a part of a list or table in Lua

I know it's very easy to do in Python: someList[1:2] But how do you this in Lua? That code gives me a syntax error.
sdamashek
  • 636
  • 1
  • 4
  • 13
18
votes
1 answer

lua: iterate through all pairs in table

I have a sparse lua table and I need to iterate over it. The Problem is, it seems that lua begins the iteration at 1, and terminates as soon as it finds a nil value. Here's and example: > tab={} > tab[2]='b' > tab[5]='e' > for i,v in ipairs(tab)…
ewok
  • 20,148
  • 51
  • 149
  • 254
17
votes
4 answers

Iterate through Lua Table

I am trying to iterate through a lua table but I keep getting this error: invalid key to 'next' I know that index starts off as -8 and I know that there is a table there because it gets the first (and only) value in it. However, it tries to loop…
Tom Leese
  • 19,309
  • 12
  • 45
  • 70
16
votes
3 answers

How to keep the order of a Lua table with string keys?

Here's an example local query = {} query['count'] = 1 query['query'] = 2 for k,v in pairs(query) do print(k) end The above will print first query then count. How can I make sure without adding an int index key that the key strings keep their…
steven
  • 662
  • 1
  • 6
  • 13
16
votes
7 answers

In Lua, how should I handle a zero-based array index which comes from C?

Within C code, I have an array and a zero-based index used to lookup within it, for example: char * names[] = {"Apple", "Banana", "Carrot"}; char * name = names[index]; From an embedded Lua script, I have access to index via a getIndex() function…
user200783
  • 13,722
  • 12
  • 69
  • 135
15
votes
2 answers

table.unpack() only returns the first element

Could somebody explain to me why table.unpack() returns the first table element only when it is used in a function call with additional parameters after table.unpack()? Here is some demo code: local a = {1,2,3,4,5} print("Test", table.unpack(a)) …
Andreas
  • 9,245
  • 9
  • 49
  • 97
1
2
3
93 94