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

Confusion with debug.getlocal in Lua

I saw here how to insert local variables in a table using the debug.getlocal function in Lua (5.1). function locals() local variables = {} local idx = 1 while true do local ln, lv = debug.getlocal(2, idx) if ln ~= nil then …
yawn
  • 422
  • 1
  • 5
  • 21
4
votes
2 answers

Special cases for table.remove

The reference manual of table.remove says: table.remove (list [, pos]) Removes from list the element at position pos, returning the value of the removed element. When pos is an integer between 1 and #list, it shifts down the elements list[pos+1],…
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
4
votes
1 answer

Lua, which way to do class-table and instantiation?

The question originated from http://tylerneylon.com/a/learn-lua/ The tutorial includes codes: Dog = {dog1 = 'original dog class'} function Dog.new(self, ... ) newObj = {sound = 'woof'} self.__index = self return setmetatable(newObj,…
Frank He
  • 75
  • 1
  • 8
4
votes
3 answers

Metamethod when accessing a key as mutable

__index is called when accessing as immutable : local foo = bar["foo"]; __newindex is called when access as mutable an index that doesn't exist : local bar = { } bar["foo"] = 123 -- calls __newindex bar["foo"] = 456 -- does NOT call __newindex Is…
Virus721
  • 8,061
  • 12
  • 67
  • 123
4
votes
4 answers

Metamethod lookup through __index?

I've implemented my own class system and I'm having trouble with __tostring; I suspect a similar issue can happen with other metamethods, but I haven't tried. (Brief detour: each class has a __classDict attribute, holding all methods. It is used as…
kikito
  • 51,734
  • 32
  • 149
  • 189
4
votes
2 answers

Lua: "dragging" sequence of elements within array

I'm trying to create a function that 'drags' a sequential number of elements to a new location within the array, constrained to the current size of the array. Other items should jiggle round the 'dragged' items. For example, if my array has 7…
cabbageforall
  • 620
  • 1
  • 5
  • 12
4
votes
1 answer

How efficient am I using these tables in lua?

So I've been scripting some Lua and when using tables I wanted to make something similar to a "node" or "class" local playerInfo = {} if player then local newPlayer = {NAME = name, HP = 10, DMG = 4} table.insert(playerInfo, newPlayer)…
krazyito65
  • 95
  • 1
  • 1
  • 9
4
votes
6 answers

Check if a Lua table member exists at any level

I need to check if a member exists in a table that isn't at the next level, but along a path of members. foo = {} if foo.bar.joe then print(foo.bar.joe) end this will cast an attempt to index field 'bar' (a nil value) because bar isn't…
Max Kielland
  • 5,627
  • 9
  • 60
  • 95
4
votes
2 answers

Appending nil to a Lua sequence

Let's say I have a sequence: a = { 10, 12, 13 } The length (#a) of this sequence is 3. Now, suppose I do the following: table.insert(a, nil) (or a[#a+1] = nil.) Will this affect the table in any way? Is the answer to this question decisive, or is…
Niccolo M.
  • 3,363
  • 2
  • 22
  • 39
4
votes
1 answer

Lua global __newindex called only once

Working through the Lua exercises in 7 More Languages in 7 Weeks and getting caught on a metatables problem. The challenge is to overload the + operator in order to be able to concatenate tables (as though they were arrays). { 1, 2, 3 } + { 4, 5, 6…
Dan Prince
  • 29,491
  • 13
  • 89
  • 120
4
votes
1 answer

C++ Lua Getting Value From Lua Table

I am trying to get values from a Lua table. This is what I have written in Program.cpp: lua_State* lua = luaL_newstate(); luaL_openlibs(lua); luaL_dofile(program->getLuaState(), "Script.lua"); lua_getglobal(lua, "table"); lua_pushstring(lua,…
Erik W
  • 2,590
  • 4
  • 20
  • 33
4
votes
1 answer

How can I implement a read-only table in lua?

I wrote an example. function readOnly(t) local newTable = {} local metaTable = {} metaTable.__index = t metaTable.__newindex = function(tbl, key, value) error("Data cannot be changed!") end …
NiklausTseng
  • 235
  • 2
  • 10
4
votes
2 answers

How to delete a table in Lua?

Let us see the following codes. do local a = {1,2,3} function a:doSth() self = nil end a:doSth() if a then print("Still has a...") end end I found that this method doesn't work. The table a still exists.…
NiklausTseng
  • 235
  • 2
  • 10
4
votes
1 answer

How to pass a table to lua with c#

How to pass a table to lua with c# I'm using the LuaInterface,this is my c# code using System; using System.IO; using System.Text; using LuaInterface; namespace GetLuaTable { class Program { static void…
Xinbs
  • 43
  • 1
  • 3
4
votes
2 answers

How to check if a table element is nil

I have a table called frameActions which could not contain some property in some case: action = 'UP' frameActions = {} frameActions['UP'] = { a = 1, b = 2 } How do I check if the table have a specific property name? if frameActions[action].c ~= nil…
vitto
  • 19,094
  • 31
  • 91
  • 130