Hi @All,
i am new to pico8, and struggeling to remove doublicate entrys in tables.
All hints i found so far, are based on the lua keywords table, witch seems to be obsolete in pico8´s lua implementation.
any tips and tricks for that problem?
regards
Rene
At some point you'll have to run through the table and check (presumably for each item in it) unless you can come up with a clever way to avoid adding them in the first place.
Also, how bad are duplicate entries in your case? Do they add that much overhead or do they significantly alter gameplay, or are you just being picky?
Another thought - at some point (like when you draw them) it might be easy to do a different check (screen pixel?) to detect duplication and remove them then.
I assume that by duplicate entries in a table, you mean duplicate items in a list, which in Lua is a table whose keys are consecutive numbers starting from 1. This is the kind of table you get from this:
x = {'a', 'b', 'c', 'b', 'd', 'a', 'e', 'f'} |
Here's one attempt:
index = {} for t in all(x) do if index[t] then del(x, t) end index[t] = true end |
This iterates over table x and maintains an index of seen items. If it finds an item it has seen, it deletes an instance of that item from the table. Note that this does not preserve item order: del() removes the first instance of the item, not a given instance. It does the de-duplication on the original table, which may or may not be desired.
Here's a version that creates a new table with the result and does not modify the original, also using a temporary index table:
result = {} index = {} for t in all(x) do if not index[t] then index[t] = true add(result, t) end end |
If you're asking about removing duplicate keys or values in a lookup table (and not a list), you can use a similar technique iterating over pairs(x), maintaining an appropriate index, and constructing a new table with the desired unique items.
[Please log in to post a comment]