How would I search this table to look for strings with the same name?
For example I wanna see how many of 1 items there are so I can stack them in an inventory system.
I assume I use i,v in pairs(t) but idk where to go from there.
(Ex: Item1 x2)
local t = {'Item1', 'Item2', 'Item1', "Item3", "Item1"}
local counts = {} -- Table to store item counts
-- Iterate & Count
for _, v in ipairs(t) do
if not counts[v] then
counts[v] = 1
else
counts[v] = counts[v] + 1
end
end
-- Print the counts for each item
for item, count in pairs(counts) do
print(item .. " x" .. count)
end