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

method for serializing lua tables

I may have missed this, but is there a built-in method for serializing/deserializing lua tables to text files and vice versa? I had a pair of methods in place to do this on a lua table with fixed format (e.g. 3 columns of data with 5 rows). Is…
cctan
  • 2,015
  • 3
  • 19
  • 29
9
votes
2 answers

How can I safely iterate a lua table while keys are being removed

In my main coroutine, I am removing or adding entries from a table depending on user operations. In the background, I'd like to iterate over the entries in the table. I don't mind particularly if I miss an insertion on one iteration, providing I can…
Eric
  • 95,302
  • 53
  • 242
  • 374
9
votes
7 answers

How can I check if a lua table contains only sequential numeric indices?

How can I write a function that determines whether it's table argument is a true array? isArray({1, 2, 4, 8, 16}) -> true isArray({1, "two", 3, 4, 5}) -> true isArray({1, [3]="two", [2]=3, 4, 5}) -> true isArray({1, dictionaryKey = "not an array",…
Eric
  • 95,302
  • 53
  • 242
  • 374
9
votes
7 answers

How do I get the highest integer in a table in Lua?

How do I get the highest integer in a table in Lua?
Jeremy
  • 111
  • 1
  • 1
  • 2
9
votes
2 answers

How do I create a Lua Table in C++, and pass it to a Lua function?

In C++, I have a map, containing an unknown number of entries. How can I pass this to a Lua function, so that the Lua function can use the data as a table?
Rocketmagnet
  • 5,656
  • 8
  • 36
  • 47
9
votes
3 answers

Returning the index of a value in a Lua table

I have this table in lua: local values={"a", "b", "c"} is there a way to return the index of the table if a variable equals one the table entries? say local onevalue = "a" how can I get the index of "a" or onevalue in the table without iterating…
Amir
  • 1,667
  • 3
  • 23
  • 42
9
votes
4 answers

How do I get first table value in Lua

Is there an easier way to do this? I need to get the very first value in a table, whose indexes are integers but might not start at [1]. Thx! local tbl = {[0]='a',[1]='b',[2]='c'} -- arbitrary keys local result = nil for k,v in pairs(tbl) do --…
Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97
9
votes
1 answer

array as parameter list lua

Today i was working some with lua,with that "oldesh" for me language,and did find what you can get arguments as array,like soo: function foo(someting,...) local arrayofargs = arg -- code here end And now,i'm intresting.Can that be in…
fhntv24
  • 384
  • 3
  • 11
9
votes
1 answer

Fast implementation of queues in Lua?

I am making a game using Lua and I need to use Breadth-first search to implement a fast path-finding algorithm which finds the shortest path between the enemy AI and the player. I will have up to 3 enemies using this algorithm at once and map is a…
Shashank
  • 13,713
  • 5
  • 37
  • 63
9
votes
1 answer

Share Array between lua and C

I have really Googled this question but I never really got an solution. I want to share an Array between C and Lua, for performance I will avoid copying Arrays to and from Lua. So I want to pass a pointer to the Array from C to Lua. And then from…
user1557786
  • 93
  • 1
  • 3
8
votes
7 answers

Lua: How to look up in a table where the keys are tables (or objects)

I want to store a lua table where the keys are other lua tables. I know that this is possible BUT I want to be able to do look ups in the table using copies of those tables. Specifically, I want to be able to do: t = {} key = { a = "a" } t[key] =…
akobre01
  • 777
  • 1
  • 10
  • 22
8
votes
5 answers

Lua - convert a table into a comma separated list

I need to convert a table into a comma separated list in order to save it to a text file. Is there a built in method for doing this in Lua?
clua7
  • 153
  • 1
  • 1
  • 9
8
votes
5 answers

how to represent nil in a table

Let's suppose I want to store a list of element. Including some nil values. The position of the values is significant, and I need to represent the absence of a value in the list at a given position. Here is a problem: a = {1,2,3,nil,4} for k,v in…
nagylzs
  • 3,954
  • 6
  • 39
  • 70
8
votes
2 answers

Convert a CSV file into a table with defined keys in Lua

I'm learning Lua to build scripts for a flight simulator. I have a CSV file that appear like this: Poti city, Poti,red,-295731.42857144,617222.85714285 Lanchhuti city, Poti,red,-299217.14285715,647851.42857142 Ozurgeti city,…
user3204845
  • 147
  • 1
  • 1
  • 5
8
votes
2 answers

Lua: set every new element in table to a default value

I want to get the most frequent k-sized substring in a string. To achieve this, I'm using a table to store the number of occurrences for each substring. Here's the code: function frequentWords(seq, k) local subs = "" local counter = {} for i =…
Fábio Perez
  • 23,850
  • 22
  • 76
  • 100