• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Array tables to Dictionary tables

Santi

Theres no way im stopping
Joined
Aug 29, 2010
Messages
1,975
Reaction score
152
Location
00
I'm currently in need of some help, is there any way to make an array table (a regular one) into a "dictionary" one?
I need to convert this:
Lua:
local t, v = {1,2,3,4}, {"1", "2", "3", "4"}
Into this:
Lua:
local table = {1="1", 2="2", 3="3", 4="4")
Or is there any way by table.insert to make this kind of tables instead of the others?
Loops wouldn't solve the problem since I need to make this tables from scratch (t and v don't exist in my case, this is just an example)
 
I barely understand you, but this might be what you want.. with loops :p.

Lua:
local t, v = {1,2,3,4}, {"1", "2", "3", "4"}
local e = {}
for (i in pairs(t)) do
 e[i] = v[i]
end
 
I barely understand you, but this might be what you want.. with loops :p.

Lua:
local t, v = {1,2,3,4}, {"1", "2", "3", "4"}
local e = {}
for (i in pairs(t)) do
 e[i] = v[i]
end

Nop, that's the same as v, I need to somehow merge t and v or merge a regular table e.g
Lua:
local t = {1,"1",2,"2",3,"3",4,"4"}
To a dictionary table like "table" shown in the first post.
 
I don't know what u mean exactly and what you mean with "dictionary" all the time is still obscure to me, but I think the last version of TGYoshi is what you meant.
Maybe the example would be more clear if you had used different indices instead of 1, 2, 3, 4 in the t-array, because that is the index of the values in the v-array anyway.

If the script above still isnt what you meant feel free to give another explanation. =)

- - - Updated - - -

There are different ways:
We always start with an empty array 't': local t = {}

table.insert(t, 2)
Result: t = {2} or t = {[1] = 2}

table.insert(t, 55, 2) -- 55 is the position in the array
Result: t = {[55] = 2}

t[55] = 2 -- Same as table.insert example

t.colorred = "#FF0000" -- Same as t["colorred"]
Result: t = {colorred = "#FF0000"} or t = {["colorred"] = "#FF0000"}

Dunno if that helped.

When index starts with a number (no matter if it is a string or a number) you can only call it with brackets, otherwise it makes no different whether you reference it with brackets or a dot after the array name
 
Last edited:
No, uhm, okay let me try in some other way, let's imagine I want to print a list like the following:
Tic tac Orange
Piano White
Ipod Grey

Fine let's do it:
Lua:
local t = {
["Tic tac"] = "Orange",
["Piano"] = "White",
["Ipod"] = "Grey"
}
--// same as my example:
local t = {tic tac="orange", piano="white" ipod="grey"

Okay so this is the way to do it, I'm aware of that (This is what I mean with dictionary table)
-
Fine, now let's say I want to make that exact thing, but by having a simple array table, just a simple table:
Lua:
local t = {"Tic Tac", "Orange", "Piano", "White", "Ipod", "Grey"}
// or as my example (which is easily converted into ^):
Lua:
local t, v = {"Tic tac", "Piano", "Ipod"}, {"Orange", "White", "Grey"}

Now, my question is how do I turn this table (the array one) into a dictionary one (the one above)

- - - Updated - - -

@Summ: That's exactly not what I mean, I'm not talking about Indexes, that's what you guys thought (passing v to e, so t indexes would be like in the table, but those are just integers not the indexes :)
 
Last edited:
So you got
local t = {}
and want it to get
local t = {
["Tic tac"] = "Orange",
["Piano"] = "White",
["Ipod"] = "Grey"
}
without defining it as this directly?

Lua:
local t = {}

t["Tic tac"] = "Orange"
t["Piano"] = "White"
t["Ipod"] = "Grey"

Wish there was a picture which said: "Still not sure if searching for basic method or algorithm"
 
Kay, I struggled a bit with the code and the answer was really simple, thanks Summ
Lua:
local s,v = {},{}
local goal = {}
for i = 1, 3 do
table.insert(s,i == 1 and "Tic tac" or i == 2 and "Piano" or "Ipod")
table.insert(v,i == 1 and "Orange" or i == 2 and "White" or "Grey")
end
for i = 1, 3 do
goal[""..s[i]..""] = ""..v[i]..""
end
 
Back
Top