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
15
votes
2 answers

Redis - Lua tables as return values - why is this not working

When I run this code through redis EVAL it return no results. Any idea why this is not working? redis-cli EVAL "$(cat bug.lua)" 0 bug.lua local retv = {} retv["test"] = 1000 return retv If I initialize the table that value alone gets printed. $…
vivekv
  • 2,238
  • 3
  • 23
  • 37
14
votes
4 answers

Sorting a Lua table by key

I have gone through many questions and Google results but couldn't find the solution. I am trying to sort a table using table.sort function in Lua but I can't figure out how to use it. I have a table that has keys as random numeric values. I want to…
Prakash.DTI
  • 913
  • 2
  • 9
  • 19
13
votes
2 answers

What is the function of square brackets around table keys in lua?

I came across tables that have square brackets around keys: local commands_json = { ["request"] = { ["application"] = PW_APPLICATION, ["push_token"] = deviceToken } } Can the square brackets be omitted?
hamobi
  • 7,940
  • 4
  • 35
  • 64
13
votes
4 answers

Lua - convert string to table

I want to convert string text to table and this text must be divided on characters. Every character must be in separate value of table, for example: a="text" --converting string (a) to table (b) --show table (b) b={'t','e','x','t'}
user3074258
  • 141
  • 1
  • 1
  • 5
12
votes
1 answer

table inside table in Lua

How can I get the data which is a table inside a table, i mean like this: t = { {a, b, c}, {d, e, f} }; if I write this line of code: print( t[1] ) the result will be —–>>> {a, b, c} BUT how can I print just the letter “a”? without using ipairs I…
Ali
  • 131
  • 1
  • 1
  • 4
12
votes
3 answers

Popping the first element off an array in Lua

I have an array x in Lua. I would like to set head = x[1] and rest = the rest of the array, so that rest[1] = x[2], rest[2] = x[3], etc. How can I do this? (note: I don't care if the original array gets mutated. In Javascript I would do head =…
Jason S
  • 184,598
  • 164
  • 608
  • 970
12
votes
1 answer

How special is the global variable _G?

Excerpt from Lua 5.3 manual: _G A global variable (not a function) that holds the global environment (see §2.2). Lua itself does not use this variable; changing its value does not affect any environment, nor vice versa. Relevant part from…
legends2k
  • 31,634
  • 25
  • 118
  • 222
12
votes
4 answers

Lua : remove duplicate elements

i am having a table in lua test = {1,2,4,2,3,4,2,3,4,"A", "B", "A"} I want to remove all duplicate elements in table. Output should be test = {1,2,4,3,"A","B"} EDIT: My try : > items = {1,2,4,2,3,4,2,3,4,"A", "B", "A"} > flags = {} > for…
Prashant Gaur
  • 9,540
  • 10
  • 49
  • 71
11
votes
1 answer

How to override color scheme in neovim lua config file?

how can I override some color scheme value in neovim lua config file? I am trying to use .lua instead of .vim. Previously in my init.vim file I have this to override some settings I want to enable these settings for init.lua file also. How I can…
monzim
  • 526
  • 2
  • 4
  • 13
11
votes
8 answers

Lua table.toString(tableName) and table.fromString(stringTable) functions?

I am wanting to convert a 2d lua table into a string, then after converting it to a string convert it back into a table using that newly created string. It seems as if this process is called serialization, and is discussed in the below url, yet I am…
Dustin Gamester
  • 792
  • 3
  • 8
  • 24
11
votes
3 answers

Pushing a Lua table

I have created a Lua table in C, but I'm not sure how to push that table onto the top of a stack so I can pass it to a Lua function. Does anyone know how to do this? This is my current code: lua_createtable(state, libraries.size(), 0); int…
Tom Leese
  • 19,309
  • 12
  • 45
  • 70
11
votes
2 answers

Lua weak reference

I'm aware of the weak tables functionality in Lua, however I would like to have a weak reference with a single variable. I've seen this proposal which suggests an API as follows: -- creation ref = weakref(obj) -- dereference obj = ref() which would…
MarkNS
  • 3,811
  • 2
  • 43
  • 60
11
votes
4 answers

Lua's hybrid array and hash table; does it exist anywhere else?

Lua's implementation of tables keep its elements in two parts: an array part and a hash part. Does such a thing exist in any other languages? Take a look at section 4, Tables, in The Implementation of Lua 5.0. Lua 5.1 Source Code - table.c
Rudiger
  • 6,634
  • 9
  • 40
  • 57
10
votes
3 answers

Lua - table.insert not working

Why isn't t:insert(9) working in Lua? (I want to append a value of 9 to the end of the table) t = {1,2,3} table.insert(t, 9) -- works (appends 9 to end of table t) t:insert(9) -- does NOT work I thought in general a.f(a,x) is equalivant to…
frooyo
  • 1,863
  • 3
  • 19
  • 21
10
votes
3 answers

are you allowed to have a numeric key in a lua table?

The following example is supposed to make a table, that can convert between a number and a string and back again, but fails to run. Is it because I'm using a numeric key in a dictionary type way? Or is it because lua starts table indices from 1? Is…
Ryan Leach
  • 4,262
  • 5
  • 34
  • 71
1 2
3
93 94