Tables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Foxat
Prole
Posts: 3
Joined: Thu Mar 21, 2013 11:50 pm

Tables

Post 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
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Tables

Post 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.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Tables

Post 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"])
User avatar
Username
Citizen
Posts: 54
Joined: Mon Jan 28, 2013 1:25 pm

Re: Tables

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Tables

Post 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
When I write def I mean function.
Foxat
Prole
Posts: 3
Joined: Thu Mar 21, 2013 11:50 pm

Re: Tables

Post by Foxat »

Thanks! I totally am grateful for all the help!!!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 6 guests