Page 1 of 1

Tables

Posted: Thu Mar 21, 2013 11:55 pm
by Foxat
Heyyyy,

I'm new to coding in general. i can't grasp and comprehend the concept of a lua table though.

Can someone please explain or provide a link that explains tables.



SHANKS :D

Re: Tables

Posted: Fri Mar 22, 2013 4:54 am
by Inny
Programming In Lua, Chapter 2.5 explains Tables. You should try to read the whole book (it's short and fast) to grasp all of Lua's concepts.

Re: Tables

Posted: Fri Mar 22, 2013 7:56 am
by T-Bone
A shorter explanation could go something like this:

Tables are a data structure. Data structures are useful whenever you want to store large amounts of stuff compactly.

Tables are a simple yet very flexible data structure that map anything to someting else. You can map numbers to stuff, which makes a table act like a simple list of things (this is what happens when you write a = {"this is a string", "this is another string", "yet another string"} ), or you can use something else as keys. The most common key, except for numbers, is strings. Because strings are so common keys, the syntax for using that is simplified. Instead of having to write a = {"x" = someValue, "y" = someOtherValue}, you can write a = {x = someValue, y = someOtherValue}.

Because of this, you can use tables sort of like objects in object oriented programming. You can let tables represent anything you want. Let's say you want rectangles in your game. You can let a table represent a rectangle. A rectangle needs an x-coordinate and a y-coordinate, as well as a width and height. So just store those values in a table! Like this

Code: Select all

rectangle = {x = 10, y = 20, width = 30, height = 40}

--access contens of recangle, the following lines are equivalent
print(rectangle.x)
print(rectangle["x"])

Re: Tables

Posted: Fri Mar 22, 2013 9:48 am
by Username
Foxat wrote:Heyyyy,

I'm new to coding in general. i can't grasp and comprehend the concept of a lua table though.

Can someone please explain or provide a link that explains tables.



SHANKS :D
Look this code:

Code: Select all

myTable = {}

for row = 1, 10 do
    myTable[row] = row
end
if you represent myTable after running that code, it will look like:

Code: Select all

myTable = {1, 
           2,
           3,
           4,
           5,
           6,
           7,
           8,
           9,
           10
          }
And to access any element you can do this:

myTable[2] = 2

Now something more complex:

Code: Select all

myTable = {}

for row = 1, 10 do
    myTable[row] = {}

    for col = 1, 4 do
        myTable[row][col] = row .. " " .. col
    end
end
That table would be :

Code: Select all

myTable = { {"1 1", "1 2", "1 3", "1 4"},
            {"2 1", "2 2", "2 3", "2 4"},
            {"3 1", "3 2", "3 3", "3 4"},
            {"4 1", "4 2", "4 3", "4 4"},
            {"5 1", "5 2", "5 3", "5 4"},
            {"6 1", "6 2", "6 3", "6 4"},
            {"7 1", "7 2", "7 3", "7 4"},
            {"8 1", "8 2", "8 3", "8 4"},
            {"9 1", "9 2", "9 3", "9 4"},
            {"10 1", "10 2", "10 3", "10 4"},
           }
Getting elements from this will be like this:

myTable[3][2] = "3 2"



A good way to build tables without messing the dimensions is to use row and col variables in the for...next instead i and j, so it is more visual and less confusing.

Hope this is helpful.

Re: Tables

Posted: Fri Mar 22, 2013 10:41 am
by kikito
I created this some time ago. It should give you a basic tour of the language. Tables are explained on page two.

https://github.com/kikito/love-tile-tut ... c-concepts

Re: Tables

Posted: Sat Mar 23, 2013 1:19 am
by Foxat
Thanks! I totally am grateful for all the help!!!