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

Lua Metatable Inconsistency

I'm having trouble understanding why there is a difference in behavior of the __index metamethod between these to examples: A = { __index = A } function A:speak() print("I'm an A") end An_A = setmetatable({},A) An_A:speak() Will raise the…
HennyH
  • 7,794
  • 2
  • 29
  • 39
5
votes
2 answers

Comparing two index tables by index-value in lua

I'm attempting to compare two tables of equal length with a function, since I don't know of any other way to do so. However, with the following function, it fails to register, and I've no clue why. I'm hoping someone can provide insight to this…
Josh
  • 3,225
  • 7
  • 30
  • 44
5
votes
1 answer

Why do I get this "attempt to call a table value" when iterating through a table?

I'm trying to find the key associated with the value of a table in Garry's mod Lua, but I'm getting an error as though it's not a table. This is part of a larger solution to a game crashing bug on someone else's code that I'm…
Fennecai
  • 109
  • 3
  • 13
5
votes
1 answer

Lua table hash indexing is faster than array indexing in my speed test. Why?

I am doing some tests to see where I can improve the performance of my lua code. I was reading this document: https://www.lua.org/gems/sample.pdf and I thought using integers as table indices should be considerably faster since it uses the array…
Mircode
  • 432
  • 5
  • 12
5
votes
3 answers

'nil' as an element in a Lua table?

I wonder if nil is a valid element in a table in Lua. What I don't understand is The following code prints 3 t = {1, 2, 3, nil}; print(#t); But the following prints 4 t = {1, nil, 3, 4}; print(#t); I don't understand why the two codes output…
Zack Lee
  • 2,784
  • 6
  • 35
  • 77
5
votes
1 answer

Write-once table in lua?

I'd like to have a write-once table in Lua (specifically LuaJIT 2.0.3), so that: local tbl = write_once_tbl() tbl["a"] = 'foo' tbl["b"] = 'bar' tbl["a"] = 'baz' -- asserts false Ideally, this would otherwise function like a regular table (pairs()…
MikeMx7f
  • 927
  • 6
  • 13
5
votes
2 answers

Lua - understanding setmetatable

I am trying to build a CNN using Torch 7. I am very new to Lua. I was trying to follow this link. I encountered something called setmetatable in the following code block: setmetatable(train_set, { __index = function(t, i) return {t.data[i],…
skr
  • 914
  • 3
  • 18
  • 35
5
votes
1 answer

Checking for items in tables Lua

I have an input file Corn Fiber 17 Beans Protein 12 Milk Protien 15 Butter Fat 201 Eggs Fat 2 Bread Fiber 12 Eggs Cholesterol 4 Eggs Protein 8 Milk Fat 5 This is loaded into a table. I can then run commands to check the value of the items. For…
Reubens4Dinner
  • 343
  • 4
  • 15
5
votes
2 answers

How to parse protobuf packets in Wireshark

My goal is to have a plugin/dissector that can parse a protocol based on protobuf (UDP). I found on the web an Auto-generate Wireshark/Ethereal dissector plugins for Protocol Buffer messages:…
B. Nir
  • 109
  • 2
  • 3
  • 12
5
votes
1 answer

How can I create a table in Lua, then add values from the C API?

Here's what I have so far... It creates global table called "mod", but I can't seem to add indexes to the table... lua_newtable(L); lua_setglobal(L,"mod");
Sam H
  • 956
  • 4
  • 16
  • 24
5
votes
2 answers

Read file line by line into array

sorry i am still learning about lua. could you correct me, why the data from file wont read line by line ? this is my example data in file points.txt : lexxo:30:1 rey:40:2 lion:40:2 prince:50:3 royal:50:3 so when i got from above is the 2d…
Han
  • 93
  • 1
  • 9
5
votes
1 answer

How to create Lua table with name C-API

How to create Lua table from C-API like this: TableName = {a, b, c} How to set table name? I only know how to create table and put values, but don't know how to set name of table. Code for creating table without name: lua_createtable(L, 0,…
BORSHEVIK
  • 794
  • 1
  • 8
  • 12
5
votes
1 answer

Lua table - two entries with same key

I'm not sure how, but we managed to create a table with two keys exactly same. When performing for loop over pairs of the table and printing keys and values we get: 1 true 1 true and we thought maybe it's a matter of different types or…
Krystian
  • 3,193
  • 2
  • 33
  • 71
5
votes
2 answers

Lua in pairs with same order as it's written

Is there any way to loop trough a table like the one below in the same order as it's written? local tbl = { ["hello"] = 1, [2] = 2, [50] = 3, ["bye"] = 4, [200] = 5 } What I mean is that when I use "in pairs" I'll get a…
Felix
  • 2,256
  • 2
  • 15
  • 35
5
votes
4 answers

How to sort two tables simultaneously by using one of tables order?

Example: table1 = {2,3,1} table2 = {a,b,c} to table1 = {1,2,3} table2 = {c,a,b}
Frank05
  • 51
  • 1