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

functions,tables and for in Lua

Right now I'm doing some tests but I cant seem to find what is wrong with this code - any idea? function IATetris(Pieza,Rotacion,Array) io.write("The table the script received has: ",Pieza,"\n") RotacionInicial = Rotacion PosInicial = 7 final =…
Makenshi
  • 993
  • 3
  • 15
  • 28
0
votes
1 answer

sorting a table in lua based on an inner tables value

So currently I have a table in Lua that contains another table (much like a hashtable). It's called email_table, and I have my person_table inside it. The email_table's keys are email_addresses and the person_table holds all information about a…
cj1098
  • 1,560
  • 6
  • 32
  • 60
-1
votes
1 answer

saving indexes of strings in lua tables (arrays or dictionary table?)

So I have quite a dilemma. I have a code that reads a certain msg, for example: m.content:sub(1,8) == 'Loot of ' then reads: 01:50 Loot of a starving wolf: a dirty fur, a salad, 2 pancakes, 60 gold Now I'm trying to make it insert into a table.…
Wesker
  • 231
  • 1
  • 3
  • 10
-1
votes
2 answers

How do I stop Lua rewriting the table variables?

I'm trying to do some OOP in Lua (newcomer), and I've come to this problem. I think it may have something to do with pointers although I can't seem to wrap my mind around it. This is the code: Atom = { px = 0, py = 0, vx = 0, vy =…
-1
votes
1 answer

Lua: moving table values into a sub table

I have a table that looks like { a = 1, b = 2, c = 3, } And I'd like to move this table, while retaining all its values, into a sub-table within the existing table, so it looks like: { letters = { a = 1, b = 2, c = 3, }, …
Sean
  • 105
  • 6
-1
votes
1 answer

Lua NGINX How to get only one value from json POST body

Hello I have lua nginx and I'm having request body My POST body is this : { "param1": "1033893", "param2": "337483", "param3": "test", } I want to cut only param2 and make it a variable, then match it with a file which it's having…
Vancho
  • 25
  • 7
-1
votes
1 answer

Adding a new field to each array element in .lua

Please tell me, if there are any errors here and how to make the code more clean. There is a space "company". Inside it's a string "type" and "information" map. Inside this map is a "job" object and an array of "users" objects. The "users" array…
Kirill Sereda
  • 469
  • 1
  • 10
  • 25
-1
votes
2 answers

Is the order of access to hashkeys in tables different between lua5.1 and lua5.3?

my question is why the key&value(512) in tables tt is not same: i have same lua code: t={501,502,503,504,505,506,507,508,509,510,511,512} tt={} for _, id in pairs(t) do print(id) tt[id] = id end print("----------------------") for k,v in…
zpx
  • 15
  • 4
-1
votes
1 answer

See if a space-seperated code in a string matches any object in a table Lua

I have a string which is a space-seperated sequence of codes such as "O R LE Pdc". The code needs to check if a code matches any object in a table (such as {"R", "BE", "Cre"}) return true, if not, return false. Here, it would return true because "R"…
-1
votes
3 answers

Is there an alternative to an if statement in Lua?

I would like to know if there is a alternative to using a normal if statement in lua. For example, in java there are switch statements but I cant seem to find that in Lua
Vish10
  • 27
  • 3
-1
votes
1 answer

How to make numbers in a table a string that isn't solved in lua?

So I'm trying to make a table with numbers in them, but also make the exact same thing but only that its a string with the numbers so doing print(table[i]) doesn't solve the problem. local problems = { 1 + 1, 2 + 2, } local stringProblems = { …
KLB
  • 1
  • 2
-1
votes
1 answer

Lua | Count Occurences of Each Item in a Table

hi I'm trying to convert this python code to Lua names=['Deepak','Reema','John','Deepak','Munna','Reema','Deepak','Amit','John','Reema'] d={} for i in range(len(names)-1): x=names[i] c=0 for j in range(i,len(names)): if…
-1
votes
1 answer

Lua table examples are not working for me

I'm trying code from lua.org and from my 4th edition Programming in Lua hardcopy, and as far as I've read all these table examples should work but 3 out of 4 don't, and I cant find anything the docs that says why. Some help on what I am missing…
Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77
-1
votes
1 answer

attempt to index a nil value items(help!)

so im gettin this error so i know theres something i probably gotta fix here but i have no idea how .thanks SCRIPT ERROR: @gcphone/server/server.lua:205: attempt to index a nil value (local 'items') CODE FROM LINE…
-1
votes
1 answer

how can we check wether similar elements are present in table in lua in minimum time complexity

if a table of N integer is present how to check if an element is repeating if present it shows message that table has repeating elements, if this is to be achieved in minimum time complexity