How do I compare the values of a table to each other?

I was wondering how I could compare the x,y cords of values in a single table. Is that possible, and if so how would I do it? Thanks! :slight_smile:

Could you post an example?

Well, I don’t have the code really yet, I was just thinking conceptually of having a table with all of my rectangles in sort of a grid and if there are three in a row of the same color they are removed. Kind of like candy crush

Sorry its not much to go off of :frowning:

Assuming you have a 3x3 2D table, you can compare their values easily enough by looping through all the rows and columns

Comparing two table entries is just like tbl[i][j] == tbl[m][n]

Ok Thanks!

Whenever I try it I just get a nil error. How many loops would I need to compare three entries?

Depends on how you are finding those entries etc - show us your code?

    for i = 1, table.maxn(blocks) do
                if blocks[i]:collision(ball) then
                    blocks[i].clr = ball.clr
                    if blocks[i].clr == blocks[i + 1].clr and blocks[i].clr == blocks[i-1].clr then
                        table.remove(blocks,i)
                        table.remove(blocks,i + 1)
                        table.remove(blocks,i - 1)
                    end
                    if table.maxn(blocks) == 0 then
                        nextLevel()
                    end
                    break
                end
            end

tell me if you need more, the blocks[i].clr calls a self.clr which is a number value from a brick class that just makes the bricks and changes colors. If you need that let me know.

if you’re getting a nil error, are you sure that blocks[i], [i+1] or [i-1] exists? you can always do
print( blocks[i] ) to see if it’s an actual object. it’ll print “table 0x0df2ad5” for example, if it’s a valid table in memory.

ok thanks ill check it!

Also, instead of table.maxn(blocks), use

for i = 1,#blocks do

--Do Something

end

end

Whats the difference?

And with this sort of code, how would I make a match 3 in a row style game?

Maybe try this?

    for i = 2, #blocks - 1 do
        if blocks[i]:collision(ball) then
            blocks[i].clr = ball.clr
            if blocks[i].clr == blocks[i + 1].clr and blocks[i].clr == blocks[i-1].clr then
                table.remove(blocks,i)
                table.remove(blocks,i + 1)
                table.remove(blocks,i - 1)
            end
             if #blocks == 0 then
                 nextLevel()
            end
        break
        end
    end

I formatted it, too.

@compactbear @CodeaNoob’s code is almost exactly the same as your old code. Using # to get the length of a table is a bit faster, I think. Anyways, most people don’t use table.maxn().

OK. Thanks. This looks good ill give it a shot!

I think #tbl and table.maxn(tbl) are equally fast, rather typing # is faster and so it is more commonly used. Neither work on indexed tables, but it is easy to write a function that does

@JakAttak Side note: they’re called indexed tables, not pairs tables. :wink:

That’s what I said… ;)) ;)) ;))