1

I have a Lua table that is automatically generating X and Y values, I am trying to make it so that if an X value is the same as another X value in the table then it does something, the something right now is not important, the only answers I am finding is if you already know what is in in the table such as "orange" "blue" which is not of interest to me.

So let's say I have loads of number being generated into a table how do I check for a matched value in that table that gets generated maybe later on in the runtime. My x, y values are already rounded to 2 decimals ie: 1.10. And my x and y values are already being generated into a table.

My code

At the start time,

positiontableX = {}
positiontableY = {}

In the runtime,

newxpos = math.floor (a[1] * 100)/100
newypos = math.floor (a[2] * 100)/100
table.insert (positiontableX, 1, newxpos)
table.insert (positiontableY, 1, newypos)

In terms of the data that is being generated in the table that is another part of the code which I don't need to add here.

David Buck
  • 3,752
  • 35
  • 31
  • 35
kepdsadef
  • 11
  • 1

1 Answers1

0

Here's an interesting if a little hamfisted solution. I'm assuming here that you don't want to overwrite any values: if you get a duplicate you want to add it to the table and keep moving along. To solve this problem, I'll be using the 'values as keys' solution to instantly check if a value exists (no looping), but using two separate tables so as not to overwrite anything.

local positiontableX = {} --an array where values are stored as values
local heldposX = {} --an array where values are stored as indexes
a = {1, 2, 3, 4, 3, 5, 7, 2, 7, 10}

for i = 1, 10 do
  local newxpos = math.floor (a[i] * 100)/100
  if heldposX[newxpos] == nil then  --if this value has never been added yet
    print("new x value " .. newxpos)
    heldposX[newxpos] = true        --add it
  else                              --if this value has been added before
    print("duplicate x value " .. newxpos)
  end
  table.insert (positiontableX, 1, newxpos) --add value to position table
end

Output

new x value 1.0
new x value 2.0
new x value 3.0
new x value 4.0
duplicate x value 3.0
new x value 5.0
new x value 7.0
duplicate x value 2.0
duplicate x value 7.0
new x value 10.0

To recap, this uses two tables for each x and y pos list. You'll add the values to each of them but use one to determine if the value is already there so you can write a script to do something special (note that in my example I add the values to the positiontableX after doing the script to do if it already exists but you could change that by moving around the lines).

Allister
  • 903
  • 1
  • 5
  • 15
  • I seem to be getting ' new x value -1.98 duplicate x value -1.98 duplicate x value -1.98 duplicate x value -1.98 duplicate x value -1.98 duplicate x value -1.98 duplicate x value -1.98 duplicate x value -1.98 duplicate x value -1.98' – kepdsadef Mar 29 '20 at 21:17
  • either A) your algorithm keeps creating duplicates or B) you're somehow looping back and keep repeating a single value. Can you post a slice of the `a` table or a full snippet of code where you're evaluating and inserting the values? – Allister Mar 29 '20 at 21:25
  • heldposX[newxpos] = true or heldposX[newxpos] = false , both work, why? – haytham-med haytham Apr 18 '23 at 10:48
  • @haytham-med haytham it's because true and false are not nil values, and the if statement only checks if there is a nil value at heldposX[newxpos]. false is not equal to nil. – Allister Apr 20 '23 at 17:51