Help understanding tables

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
ixjackinthebox
Prole
Posts: 45
Joined: Sun Apr 29, 2012 6:00 pm

Help understanding tables

Post by ixjackinthebox »

Help understanding tables I have read this http://lua-users.org/wiki/TablesTutorial
but I still don't really understand can some one explained it or could I maybe have a example. (plz)
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Help understanding tables

Post by Jasoco »

Pretty much everything in Lua is part of a table. A table is an "array" of sorts which is data in a list, or "table". Tabled can contain anything at all. Numbers, Text strings, Boolean values, Functions...

Code: Select all

--This is a sample table called "table". Right now it's empty.
table = {}

--This is a variable. In this example it is a string containing "something".
variable = "something"

--The two are interchangable and can be named almost anything you want.

--When a variable is empty, it is "nil". So if you were to do the following...
print(variable_that_does_not_exist)
--You will get "nil" because there's nothing there.

Code: Select all

--Tables are formed as such:
table = {
  "some data", --Method A
  [3] = 55,  --Method B
  a_boolean = true, --Method C
  ["functions_rock"] = function(value) print("This was a function " .. value) end, --Method D
  a_table = { 1, 4, 22, 74 } --Method E
}
Method A: If you don't specify an index, the values will be inserted numerically starting at 1. So in this case, table[1] = "some data".

Method B: You can also specify one yourself. In this case, table[3] = 55.

Method C: Indexes can also be named. In this case, table.a_boolean = true

Method D: You can also place the name as a string in brackets. It doesn't matter.
In this case, table.functions_rock is the same as table["functions_rock"]. This also applies to Method C above.
In this case, the value is a function which won't return as a value, but can be called like you would a function like so: table.functions_rock("String value") would call the function above while also sending an optional argument which would in this case simply print "This was a function String value" to the console.

Method E: This is another table. Tables can contain more tables if they want.
You would refer to the first value in this example as table.a_table[1] = 1.

That's the best I can do in layman's terms.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Help understanding tables

Post by Roland_Yonaba »

ixjackinthebox wrote:Help understanding tables I have read this http://lua-users.org/wiki/TablesTutorial
but I still don't really understand can some one explained it or could I maybe have a example. (plz)
As you might be aware of, there is pretty much to say about tables in Lua, and the way they work.
Maybe actually you could be a little more specific about what you do not get ?
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests