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

Passing table without parentheses in lua giving error

The following gives me an error 'syntax error near 'Person' even though in the faq (http://www.luafaq.org/) it stats: "so it cleverly uses the fact that Lua will accept single function arguments without parentheses if the argument is a string or a…
user441521
  • 6,942
  • 23
  • 88
  • 160
5
votes
3 answers

Lua sort table alphabetically, except numbers

I would want to sort a table alphabetically. Except numbers. The code below shows how the table is sorted with comparator function: function( a,b ) return a.N < b.N end Gives me: obj = { [1] = { ["N"] = "Green 1"; }; [2] = { …
Dreanh
  • 53
  • 4
5
votes
1 answer

Lua execute something stored in tables key value

I want to make a table with specific names as keys and specific functions as values. The key names represent commands that a user enters and if a key by that name exists, then the program should execute the code stored in that keys value. So for…
thee
  • 53
  • 4
5
votes
3 answers

Is ipairs reliable on unsorted arrays?

I'm wondering if anyone can confirm whether you can trust ipairs() to; return all indices in order, for a table that's index-complete but unsorted. We have code all over our project that clones tables using pairs(), however any arrays cloned come…
CalvinE
  • 191
  • 1
  • 7
5
votes
3 answers

In Lua, can I easily select the Nth result without custom functions?

Suppose I am inserting a string into a table as follows: table.insert(tbl, mystring) and that mystring is generated by replacing all occurrences of "a" with "b" in input: mystring = string.gsub(input, "a", "b") The obvious way to combine the two…
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
5
votes
1 answer

Remove multiple elements from array in Lua

In Lua, I know there is table.remove(array, index) Is there a fast way to remove and return X elements from the array (without just repeated calls to table.remove)?
K2xL
  • 9,730
  • 18
  • 64
  • 101
5
votes
3 answers

table.insert/remove by value

I got two tables, for example: table1 = { element1, element2, element3, element4 } table2 = { element1, element3 } Table 2 refers to some elements of table1, but I don't know which exactly, nor I know their index. Now, for an specific element I…
jawo
  • 856
  • 1
  • 8
  • 24
5
votes
1 answer

Lua table length function override not working

How does one change the length operator (#) for a table in Lua, the manual suggests assigning the __len function in a metatable and then assigning that metatable to the table I want to override, but this doesn't work as expected? I do not have the…
fisherdog1
  • 65
  • 5
5
votes
1 answer

Lua table access efficiency

I have a question about accessing data in a Lua table. Say, there is a large Lua table like following: tbl = { { blockIdx = 5, key1 = "val1", key2 = "val2", ... }, { …
Wayne Dimart
  • 85
  • 1
  • 8
5
votes
1 answer

how to play with a function which return table in lua?

i am not able to handle table which is return by a function.can any one help me on this ? local grades = { Mary = "100", Teacher="100",'4','6'} print "Printing grades!" grades.joe = "10" grades_copy = grades for k, v in ipairs(grades) do -- print…
Bee..
  • 195
  • 1
  • 8
5
votes
3 answers

Lua table sort does not work

I have the below program code which tries to sort a given list. I have tried various options and it still doesn`t work. local List = {} List[143] = "143" List[145] = "145" List[120] = "120" List[178] = "178" …
user2951607
  • 61
  • 1
  • 3
5
votes
2 answers

Returning key of maximum or minimum number in a table

A simple Lua problem here: how to find the index, or key, of the minimum or maximum number in a given table. math.max/math.min only gives the actual max or min number, not the key.
Lucien S.
  • 5,123
  • 10
  • 52
  • 88
5
votes
2 answers

create table from two input tables .output table's key will be from first input and values will be from second input table

I am having a table in which I am passing names: names = {'Sachin', 'Ponting', 'Dhoni'} and in other table I am passing country names: country = {"India", "Australia", "India"} I want output table like: out_table = {Sachin="India",…
Prashant Gaur
  • 9,540
  • 10
  • 49
  • 71
5
votes
2 answers

Copying a global table across Lua states

I have a global table, which, I want to keep in sync, between two different Lua states. From what I have read, and understood, the only way seems to be, in my C back-end, do a deep copy of the table between the states (if the table has been…
Ani
  • 1,448
  • 1
  • 16
  • 38
5
votes
2 answers

Why the function in table is called even if it's not selected

As a beginner I have a kind of simple problem in Lua: a = function() print("hello") end b = {125, 116, a()} print(b[1]) it should only print 125, but also prints hello too. even if table value is not selected.