Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a 2D array in Lua?

Tags:

arrays

lua

How can I make a 2D array with Lua? I need to dynamically create this.

local tbl = { { } }

Something like the above but where I can specify how many items. In my case they'll be the same amount. I basically want to access it like tbl[3][5].

Thanks

like image 790
user441521 Avatar asked Jan 08 '12 20:01

user441521


People also ask

How do you declare an array in 2D?

To declare a 2D array, specify the type of elements that will be stored in the array, then ( [][] ) to show that it is a 2D array of that type, then at least one space, and then a name for the array. Note that the declarations below just name the variable and say what type of array it will reference.

Do Lua arrays start at 0 or 1?

Yes, the arrays in Lua start with index 1 as the first index and not index 0 as you might have seen in most of the programming languages.


1 Answers

-- Create a 3 x 5 array
grid = {}
for i = 1, 3 do
    grid[i] = {}

    for j = 1, 5 do
        grid[i][j] = 0 -- Fill the values here
    end
end
like image 54
TheBuzzSaw Avatar answered Oct 02 '22 06:10

TheBuzzSaw