#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Search table

edited September 2013 Posts: 20

How do i compare the result of a sum with all the values in a table?
Ex.
t={2,6,8,9}
a=math.random(1,10)
b=math.random(1,10)
c=a-b
c must be one of the values in table t

Tagged:

• Mod
Posts: 3,295
``````for i,v in pairs(t) do
if v == c then print('index '..tostring(i)..' matches') end
end
``````
• Mod
Posts: 7,810
``````function setup()
t={2,6,8,9}
a=math.random(1,10)
b=math.random(1,10)
c=a-b
for x,y in pairs(t) do
if y==c then
print(a.." - "..b.." equal to "..y)
else
print(a.." - "..b.." not equal to "..y)
end
end
end

``````
• Posts: 20

I only wont to print a-b=c.
It should always be true
If it is not true it should repeat

• Mod
edited September 2013 Posts: 7,810

@Aaronwillaert Sorry, but I'm not sure what you're asking. I went ahead and did this anyways.

``````displayMode(FULLSCREEN)

function setup()
t={2,6,8,9}
next=false
end

function draw()
background(40,40,50)
fontSize(40)
fill(255)
if not next then
a=math.random(1,10)
b=math.random(1,10)
c=a-b
for x,y in pairs(t) do
if y==c then
next=true
end
end
else
text(a.." - "..b.." = "..c,WIDTH/2,HEIGHT/2)
text("tap screen for next display",WIDTH/2,HEIGHT/2-100)
end
end

function touched(t)
if t.state==BEGAN then
next=false
end
end

``````
• Posts: 20

That's it, thanks!!

• Posts: 20

The solution can be so simple

• Posts: 20

How come if i remove one number with every touch at the end the text is displayed very slow

• Mod
Posts: 7,810

@Aaronwillaert I assume by removing one number, you're removing one number from the table t. a and b are random numbers. a - b has to equal c, and c has to be a number in the table t. So each time you remove a number from the table t, there are less numbers to match. So it takes longer for a and b to form a correct equation. Each time the draw function is called ( 60 times per second ), random numbers are assigned to a and b, and the result of a - b is compared to a number in the table. So that causes the display to look slow. The code can be changed and a while loop added so that all the random calculation are done in a single draw loop. I'll add that code later unless you want to try it yourself. You have to be careful with while loops in draw, if the while loop doesn't exit, draw doesn't exit and it appears like the iPad is locked up.

• Mod
Posts: 7,810

@Aaronwillaert Here is the code using a while loop in draw.

``````displayMode(FULLSCREEN)

function setup()
t={2,6,8,9}
done=false
end

function draw()
background(40,40,50)
fontSize(40)
fill(255)
while not done do
a=math.random(1,10)
b=math.random(1,10)
c=a-b
for x,y in pairs(t) do
if y==c then
done=true
end
end
end
text(a.." - "..b.." = "..c,WIDTH/2,HEIGHT/2)
text("tap screen for next display",WIDTH/2,HEIGHT/2-100)
end

function touched(t)
if t.state==BEGAN then
done=false
end
end

``````
• Posts: 20

So you mean when my table t is empty is will crash because "done" can't become true, so the while continues for infinity (trying to understand)

• edited September 2013 Posts: 2,161

Throwing out ineligible pairs gets inefficient as the number of eligible pairs gets small relative to the total number. Better is to create a list of all eligible pairs and then select one of those at random.

``````t = {2,6,8,9}
s = {}
for k=1,10 do
for _,v in ipairs(t) do
if k + v > 10 then
break
end
table.insert(s,{k+v,k})
end
end
i = math.random(1,#s)
a = s[i][1]
b = s[i][2]
print(a .. " - " .. b .. " = " .. a - b)
``````
• Posts: 20

Thanks just what i need. Beter then the while loop.
took a while before I understood it. i have much to learn.