Log In  

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

P#31308 2016-10-20 13:09 ( Edited 2016-10-21 13:30)

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.

P#31313 2016-10-20 13:44 ( Edited 2016-10-20 17:44)

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.

P#31315 2016-10-20 13:45 ( Edited 2016-10-20 17:45)

Hey!
Thank very much, the last postiing fits perfectly!

P#31379 2016-10-21 09:30 ( Edited 2016-10-21 13:30)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2023-02-02 08:44:50 | 0.021s | Q:15