Hey there!
So I want to check if there are three different values in a table but I don’t know how.
for _, v in pairs(adventurerList) do
for _, val in pairs(adventurerList) do
if v ~= val and adventurerCounter == 0 then
print("Adventurer I")
adventurerCounter = 1
end
end
end
I may be misunderstanding this, but what I would do is go through the table you are checking and add each value to a temporary table, each time you add a new value to the temporary table check if it already exists in the table. If it doesn’t then you can add it. You can also check the length of it each time to check if you have 3 values in the table.
Hope this helps you.
local Table = {'Adventurer1', 'Adventurer2', 'Adventurer2', 'Adventurer3'}
local function CheckTable(PassedTable)
local TempTable = {}
for _,Value in pairs(PassedTable) do
if not table.find(TempTable, Value) then
table.insert(TempTable, Value)
else
return(false)
end
end
return(true)
end
print(CheckTable(Table))
This would return false because Adventurer2 is repeated, again, not sure if this is what you wanted the question is slightly confusing.
Not sure why you are looping through a table inside the same table, but I believe you are trying to get the number of elements inside a table. For that, you could either use #table or table.getn(table)
What I’m trying to do is like if you have a table with for example {“Something”, “Another”} there are two different values so it will print like “2” and {“Something”, “Another”, “AnotherOne”} then it will print “3” but there can be like two of the same thing so I want to check only the number of the different things in the table. Sorry if it was too confusing.
hmm… i’d suggest the method that @r_apt used, basically just moving it to another table to store non-duplicate values
in this example, adventurerList has 4 objects, including 2 duplicates
local adventurerCount = 0
local nonDuplicates = {}
local function getNumObjects()
for i, v in pairs(nonDuplicates)
adventurerCount += 1
end
return(adventurerCount)
end) -- i forgot if you add a paranthesis or not
for_, v in pairs(adventurerList) do
if not table.find(nonDuplicates, v) then
table.insert(nonDuplicates, value)
end
end
local objectNum = getNumObjects()
print(objectNum) -- should be 3!