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
173
votes
9 answers

Why do Lua arrays(tables) start at 1 instead of 0?

I don't understand the rationale behind the decision of this part of Lua. Why does indexing start at 1? I have read (as many others did) this great paper. It seems to me a strange corner of a language that is very pleasant to learn and program.…
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
153
votes
5 answers

How to check if a table contains an element in Lua?

Is there a method for checking if a table contains a value ? I have my own (naive) function, but I was wondering if something "official" exists for that ? Or something more efficient... function table.contains(table, element) for _, value in…
Wookai
  • 20,883
  • 16
  • 73
  • 86
146
votes
8 answers

Most efficient way to determine if a Lua table is empty (contains no entries)?

What's the most efficient way to determine if a table is empty (that is, currently contains neither array-style values nor dict-style values)? Currently, I'm using next(): if not next(myTable) then -- Table is empty end Is there a more…
Amber
  • 507,862
  • 82
  • 626
  • 550
126
votes
2 answers

How to remove a lua table entry by its key?

I have a lua table that I use as a hashmap, ie with string keys : local map = { foo = 1, bar = 2 } I would like to "pop" an element of this table identified by its key. There is a table.remove() method, but it only takes the index of the element to…
Wookai
  • 20,883
  • 16
  • 73
  • 86
104
votes
3 answers

How do I append to a table in Lua

I'm trying to figure out the equivalent of: foo = [] foo << "bar" foo << "baz" I don't want to have to come up with an incrementing index. Is there an easy way to do this?
drewish
  • 9,042
  • 9
  • 38
  • 51
86
votes
10 answers

How to merge two tables overwriting the elements which are in both?

I need to merge two tables, with the contents of the second overwriting contents in the first if a given item is in both. I looked but the standard libraries don't seem to offer this. Where can I get such a function?
RCIX
  • 38,647
  • 50
  • 150
  • 207
84
votes
5 answers

How to iterate through table in Lua?

So, I have a table something along these lines: arr = { apples = { 'a', "red", 5 }, oranges = { 'o', "orange", 12 }, pears = { 'p', "green", 7 } } It doesn't seem like it's possible to access them based on their index, and the values…
Lemony Lime
  • 1,113
  • 3
  • 11
  • 12
69
votes
12 answers

Search for an item in a Lua list

If I have a list of items like this: local items = { "apple", "orange", "pear", "banana" } how do I check if "orange" is in this list? In Python I could do: if "orange" in items: # do something Is there an equivalent in Lua?
Jay
  • 1,423
  • 2
  • 11
  • 9
67
votes
16 answers

How do you copy a Lua table by value?

Recently I wrote a bit of Lua code something like: local a = {} for i = 1, n do local copy = a -- alter the values in the copy end Obviously, that wasn't what I wanted to do since variables hold references to an anonymous table not the values…
Jon 'links in bio' Ericson
  • 20,880
  • 12
  • 98
  • 148
55
votes
15 answers

Concatenation of tables in Lua

ORIGINAL POST Given that there is no built in function in Lua, I am in search of a function that allows me to append tables together. I have googled quite a bit and have tried every solutions I stumbled across but none seem to work properly. The…
John Mark Mitchell
  • 4,522
  • 4
  • 28
  • 29
53
votes
2 answers

lua: retrieve list of keys in a table

I need to know how to retrieve the key set of a table in lua. for example, if I have the following table: tab = {} tab[1]='a' tab[2]='b' tab[5]='e' I want to be retrieve a table that looks like the following: keyset = {1,2,5}
ewok
  • 20,148
  • 51
  • 149
  • 254
46
votes
3 answers

How to quickly initialise an associative table in Lua?

In Lua, you can create a table the following way : local t = { 1, 2, 3, 4, 5 } However, I want to create an associative table, I have to do it the following way : local t = {} t['foo'] = 1 t['bar'] = 2 The following gives an error : local t = {…
Wookai
  • 20,883
  • 16
  • 73
  • 86
38
votes
3 answers

Check if array contains specific value

I have this array, with some values (int) and I want to check if a value given by the user is equal to a value in that string. If it is, output a message like "Got your string". Example of the list: local op = { {19}, {18}, {17} } if 13 == (the…
Ether Metin
  • 391
  • 1
  • 3
  • 4
34
votes
4 answers

What's the difference between table.insert(t, i) and t[#t+1] = i?

In Lua, there seem to be two ways of appending an element to an array: table.insert(t, i) and t[#t+1] = i Which should I use, and why?
Eric
  • 95,302
  • 53
  • 242
  • 374
34
votes
3 answers

How to 'unpack' table into function arguments

I'm trying to call a function in Lua that accepts multiple 'number' arguments function addShape(x1, y1, x2, y2 ... xn, yn) and I have a table of values which I'd like to pass as arguments values = {1, 1, 2, 2, 3, 3} Is it possible to dynamically…
codinghands
  • 1,741
  • 2
  • 18
  • 31
1
2 3
93 94