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

lua language - attempt to call global

i have 2 array classMate and winners, in need to check if winners in classMate, if yes put res[i] = 1 function win(classMates , winners) res = {} for i=1,#winners do for j=1,#classMates do print(3) if winners[i]…
Nicholas
  • 39
  • 6
-1
votes
1 answer

Spliting tables into 9's

I would like to know how can I split my table into subtables of 9's. Example: { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 } Code shall return: { {1, 2, 3, 4, 5, 6, 7, 8, 9}, {10, 11, 12, 13, 14, 15, 16, 17, 18}, { 19,…
Ram Jano
  • 23
  • 2
-1
votes
1 answer

Lua table.insert did not work

So recently I made this code where ill call a function to insert the data into the table, however it did not insert the data but does not return any errors, any problem with this? datas = {} function…
itme93
  • 39
  • 2
-1
votes
1 answer

Different behavior when assigning a function to a variable or a table

I have got a different behavior between a table and a simple variable assignation with a function. This difference does not exist when assigning a number for instance. -- works fine arrNum={1234} Num=arrNum[1] -- Num=1234 arrNum[2]=arrNum[1] --…
user1771398
  • 475
  • 6
  • 18
-1
votes
1 answer

"attempt to call table" without for

When I call the method stream that I made, with any table as argument, I get the error "attempt to call table". As far as I know this error only occurs when I use a for wrong, but I don't have a for in the code that is executed... function…
gDKdev
  • 3
  • 3
-1
votes
1 answer

Corona SDK, How to save String in leaderboards?

I'm having a problem with my leaderboards, the problem is my array score are all sorted in ascending order and works fine but I can't save the text beside the score? The situation is game.lua --> gameOver(Score & Difficulty Text) -->…
Kilik Sky
  • 151
  • 1
  • 1
  • 12
-1
votes
1 answer

When change lua table value?

Three loops but different results, why? (in lua 5.1) 1. local a = {{b=5}, {b=4}} for k,v in ipairs(a) do v.b = v.b + 1 end 2. local a = {["b"]=5, ["bb"]=4} for k,v in pairs(a) do v = v + 1 end 3. local a = {5, 4} for k,v in ipairs(a) do …
jammyWolf
  • 2,743
  • 1
  • 13
  • 7
-1
votes
1 answer

Understanding nested for/in loops with "ipairs"

Can some body help me to understand this piece of code? local dev for _, dev in ipairs(devices) do local net for _, net in ipairs(dev:get_wifinets()) do netlist[#netlist+1] = net:id() netdevs[net:id()]…
-1
votes
1 answer

Lua issue with table length?

I was messing around with Lua tables and I noticed: local t1 = {1, 5, nil, 10} local t2 = {1, 5, nil, 10, nil} print(t1[5], t2[5]) --> nil nil print(#t1, #t2) --> 4 2 I was expecting the length of both tables to be 4, but the length of t2…
David
  • 693
  • 1
  • 7
  • 20
-1
votes
2 answers

Function arguments table and key value

I am trying to make a table, name given by the first arg of a function and assign a value to a key named by the second arg. For example function myinsert(a, b) a.b = 10 For example when I give args pencil and price, pencil table to be created…
user2276872
-1
votes
2 answers

Looping a Lua table matching for a specific key then setting the value to a variable

So I have a table that looks something like this: local weapons = {[46]= "Megapositron", [173]= "Sunflare", [702]= "raven"} I also have a variable: weaponid The variable weaponid will contain one of the keys from weapons (46,173,702) What I can't…
moretimetocry
  • 73
  • 1
  • 1
  • 4
-1
votes
1 answer

Lua table add an exception

How can I set a value in my grid (for example address : grille[4][5]) directly in a loop? I would like that grille[4][5].yScale and grille[4][5].xScale are smaller than the others. grille = {} local colonnes = 8 local lignes = 15 local variable =…
espace3d
  • 23
  • 10
-1
votes
2 answers

What feature of table.sort allows it to sort an array by the values of an associative array?

I can't wrap my head around why record[x] would match up it's key name to a string in an array, then take it's value as the item to index the string by.. Is this some special feature of table.sort? list = {"b", "c", "a"} record = {a = 1, b = 2, c =…
Igorio
  • 918
  • 1
  • 7
  • 17
-1
votes
1 answer

Iterate through LUA Table for string match

I'm trying to match two tables by string case in sensitive. Example [6] = { ["itemName"] = [Ore], ["cleanName"] = [[Iron Ingot]], ["secs"] = 25004, …
-1
votes
1 answer

Receiving error when string.reverse items of a table

I'm currently using equations to enter items into a table. I then want to check if the items in v are the same when string.reverse and if so, print that out. table.insert (t,#t+1,z) for k,v in ipairs (t) do if string.reverse(v) == v then …
Pwrcdr87
  • 935
  • 3
  • 16
  • 36