• 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 Problem with tabble

Critico

Sexy
Joined
Mar 25, 2010
Messages
370
Reaction score
176
I need to return the total table

Lua:
tabble = {
["minotaur"] = {exp = 1000},
["globin"] = {exp = 2000},
["rotworm"] = {exp = 4000}
}

total: 3 ^

but if use '#tabble' returns 0


one way is as follows:
Lua:
tabble = {
["minotaur"] = {exp = 1000},
["globin"] = {exp = 2000},
["rotworm"] = {exp = 4000}
}

local x = {}
for var, ret in pairs(tabble) do
table.insert(x, var)
end

print(#x) -- return 3



does anyone know another way?
 
Hmm, this:
Lua:
tabble = {
["minotaur"] = {exp = 1000},
["globin"] = {exp = 2000},
["rotworm"] = {exp = 4000}
}
for k,v ipairs(tabble) do
...

btw... your last example was the way to solve it.
 
but has no other way instead of using ipairs?
Lua:
tabble = {
["minotaur"] = {exp = 1000},
["globin"] = {exp = 2000},
["rotworm"] = {exp = 4000}
}
 
local x = {}
for i=0, #tabble - 1 do
  table.insert(x, tabble[i]) -- I suspect something is wrong here in this line. if it is returning 0
  print(i) -- Test Line to see if #tabble is being read properly
end
 
print(#x) -- return 3
 
#table only counts when the index it's a positive number and I think there isn't a pre-made function in lua to count string indexes.

Code:
tabble = {
["minotaur"] = {exp = 1000},
["globin"] = {exp = 2000},
["rotworm"] = {exp = 4000}
}

i = 0
for _, _ in pairs(tabble) do
	i = i + 1
end

print(i) -- 3
 
Back
Top