Looping through 2D tables

I have a table that looks something like this:

table = {
    [1] = {
        [1] = 0,
        [2] = 0
    },

    [2] = {
        [1] = 0,
        [2] = 0
    }
}

What I need to do is loop through the table so that the index table[1][1] is grabbed, and then index table[2][1], and so on. Then, I need to loop through the next so that index table[1][2] is grabbed and then index table[2][2].

Let me know if you need any other information for how this should work.

Right now I have a system that loops through the array vertically:

for i, column in pairs(table) do
	for j, space in pairs(column) do
		local index = Vector2.new(i, j)
        return index
	end
end

Essentially I just need help understanding how to make a function that would do the same thing horizontally across the table, looping through each row instead of each column.

What is “index” essentially returning to?

1 Like

The index of the array, essentially the equivalent of a Vector2 in terms of formatting.

index.X would be the column, index.Y would be the space in the column.

You could probably make a table called scraps, which any unneeded values can be stored in, and eventually cleared when the loop is broken or has been breaked.

I’m confused. How does this relate to what I need? What “unneeded values” would I need to store?

[1][2], [2][2], and so on.

(which you have stated on your post)

Right. So you are saying store those values in a table. What do I do with them though? I am attempting to loop through the table in a certain way, I just don’t know how. That is what I need help understanding. How should I loop through the table so that the order is like so:

index[1][1], index[2][1], index[1][2], index[2][2], -- and so on

You could probably make your own algorithm for looping. By the looks of it, it looks like your second table value goes up by one each two logs. For your first table, it looks like it goes back to back.

I ended up figuring this out on my own, but thank you for your help.

For those wondering (literally no one) here is what I did:

for i = 1, #grid_table[1] do
	for j = 1, #grid_table do
		local index = Vector2.new(j, i)
		return index
	end
end

This iterates through the number of rows (#table[1]) and then for each row, iterates through the number of spaces in each row (#table). The index would then be the column, j, and the space, i.

1 Like

That’s what I said to do, but alright. You’ve made your own looping algorithm.

You told me to make my own looping algorithm. How is that helpful? There was no instruction at all other than essentially saying “make your own loop”.

i was in the processing of making a loop but i saw that you solved it :sweat_smile:

heres what i have:

for a = 1 , #table do
	local index = table[a]
	for i = 1 , #index do
		local g = Vector2.new(i,a)
		print(g)
	end
end
1 Like

The problem with this is that it is still looping through each column (#table), instead of each row in the column (#table[1]).

Using #table[1] as the outer loop, it gets all of the rows. Using #table in the inner loop, it gets all of the spaces in each row.

#table = columns
#table[1] = rows per column

EDIT: I take back what I have said. My solution does not work as intended. I am still testing your solution.

Okay. Both of our solutions work actually. It turns out there was something else causing them to not run properly. Thank you for your help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.