Doesn’t work. Just returns 0. So what then is the easiest way to get the amount of items from a dictionary? I’ve heard metatables be thrown around, but I’d like to avoid looking into those at all costs
Items = {
['Sword'] = {Stick = 1, Wood = 2),
['Axe'] = {Stick = 2, Wood = 2),
['Stick'] = {Wood = 2}
}
for key, value in pairs(Items) do
print(key.."this is the key")
print(value.."this is the value")
print(key, value)
end
i think the only real (and practical) way to get the length of a dictionary is to use a counter/iterator function
local function Length(Table)
local counter = 0
for _, v in pairs(Table) do
counter =counter + 1
end
return counter
end
local Table = {test1 = 5, test2 = 3}
print(Length(Table)) --->>> 2
print(table.getn(Table)) ---->>> 0
print(#Table) -->>> 0
Edit: if you are wanting the number of items in total then you would change counter =counter + 1 to something like this:
counter = counter + ( tonumber(v) or 0)
Then the output would be:
print(Length(Table)) --->>> 8
if for some reason you wanted to get the total amount in a dictionary no matter how deep it is, you could use some recursion
How can I easily get the items from the dictionary then as well?
local Items = Length(v.Recipe)
if Items == 1 then
Requirements['1'].Visible = true
Requirements['2'].Visible = false
Requirements['3'].Visible = false
print(v.Recipe[1])
end
Let’s say v.Recipe = {Wood = 2} I want the print v.Recipe[1] to print Wood. I can’t do v.Recipe.Wood tho as recipes could use other materials
i don’t think there really is a way to do this because a Dictionary’s order can be quite arbitrary, at least in a numerical sense. One way, if you wanted to is, you could convert the table 's indexes to number using this:
But keep in mind it wouldn’t most likely be in order or what you necessarily want in all scenarios
local function ConvertTable(Table)
local index = 0
local NewTable = {}
for _, v in pairs(Table) do
index = index + 1
NewTable[index] = {Name = _, Value = v}
end
return NewTable
end
local TestTable = ConvertTable(v.Recipe)
print(TestTable[1].Name) ----->>>> Wood
Although you could use this, i would rather use numerical indexes to begin with then you could discard the Length function( if you just needed the number of elemets in table) and you could easily get items from the table:
I tried reading up on OnlyJaycbee’s solution but that didn’t seem to apply to what I needed…
I’m not sure if anyone would need this but here’s a function that will give you the deep total values a table has.
local B_Upgrades = {
{1,3,5}, -- 3 values
foo = 1; -- 1 value
barfoo = {4,"allen"}; -- 2 values
cuckoo = {{1,true},{4,"tuck"}} -- 4 values
-- 10 Total Values
}
local function GetTotalV(Table)
local counter = 0
for i,v in pairs(Table) do
if typeof(v) ~= "table" then
counter += 1
else
counter += GetTotalV(v)
end
end
return counter
end
local x = GetTotalV(B_Upgrades)
print(x)
--[[ Output
21:56:08.248 10 - Client - Sample:37
]]
In my case I use this to check if a player has bought an upgrade and will do