Easiest way to get amount of items in dictionary

I know for example:

Items = {
    ['Sword'] = {Stick = 1, Wood = 2),
    ['Axe'] = {Stick = 2, Wood = 2),
    ['Stick'] = {Wood = 2}
}

#Items.Sword

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 :grimacing:

2 Likes

This might help (I haven’t really done much with dictionaries, but hopefully this might help you.

Items = {
    ['Sword'] = {Stick = 1, Wood = 2),
    ['Axe'] = {Stick = 2, Wood = 2),
    ['Stick'] = {Wood = 2}
}

--To use the dictionary, you might use something like this:
print(Items["Sword"])

Please let me know if this helps or not.

Problem is I have multiple stuff inside the dictionaries

['Wooden Axe'] = {
			Image = 'rbxassetid://258100839',
			Type = 'Axe',
			
			Recipe = {Wood = 2, Stick = 2},

Just a cut of it. I want to get how many objects are in the Recipe.

You could also try using this:

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
1 Like

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

17 Likes

In that case, I don’t really know how to do that, but maybe someone else could help you.

How can I easily get the items from the dictionary then as well? :thinking:

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:

Items = {
    ['Sword'] = {{"Stick", 1}, {"Wood", 2}},
}
print(#Items.Sword) ---->>> 2
print(Sword[1][1]) --->>>> Stick
print(Sword[1][2]) -->>> 1

if you still need to use the counter function for getting the total amount of items you can replace:

counter = counter + ( tonumber(v) or 0)

with:

counter = counter + ( tonumber(v[2]) or 0)

1 Like

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

local CashPerSecond *= 2 ^ x
1 Like