• Checking For Duplicates In A Table.
    2 replies, posted
How would I check if there are two of the same values in one table. [lua] for k, v in pairs(soundBlockList) do for x, y in pairs(soundBlockList) do if v == y then table.remove(soundBlockList, x) end end end [/lua] It sees and deletes itself :/
Try also checking if the key is different. So if the the values are the same, then check if the keys are the same. if not table.remove [lua] for k, v in pairs(soundBlockList) do for x, y in pairs(soundBlockList) do if v == y && k != x then table.remove(soundBlockList, x) end end end [/lua]
Looks like you're doing a list of blocked sounds. In this case, you better store the sound as the key, and anything you want as the value. So instead of doing this: [lua]soundBlockList[1] = sound[/lua] You do that: [lua]soundBlockList[sound] = true[/lua] This way, you are sure there will never be duplicates, and if you ever need to verify if a sound is blocked, all you have to do is check if soundBlockList[sound] is true, instead of iterating through the whole table.
Sorry, you need to Log In to post a reply to this thread.