Tables for Dummies

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
User avatar
PedroChurro
Prole
Posts: 25
Joined: Tue Oct 09, 2012 8:22 pm
Location: In my room

Tables for Dummies

Post by PedroChurro »

Can someone explain me tables "for Dummies" style? Thanks!
I try to make games.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Tables for Dummies

Post by Lafolie »

A table is a fun, easy way to access and allocate memory. Tables are lua's greatest asset and cover the functionality of several constructs as used in other languages.

Most simply put, a table can be used as a list, array or dictionary (although there are many, many more way to use them!). We can think of these structures as containers for other things (variables), including more tables.

You declare a table like this:

Code: Select all

foo = {}
Now we can begin 'putting things into' our table. There are several ways to do this.

During the declaration:

Code: Select all

foo = {1, 2, "a", bar = "9001"}
With an element reference:

Code: Select all

foo[1] = "Hello!"
Using table.insert:

Code: Select all

table.insert(foo, "World!") --table.insert(table, value)
We can use all sorts of functions, loops and iterations to modify and play with our table, the simplest of which is the table.remove:

Code: Select all

table.remove(foo, 1) --remove element 1 from table 'foo'
When we need to reference something stored in a table we have many options also. Here's a simple table and some different ways to access it's elements:

Code: Select all

foo = {1, 2, 3, foo = "bar"}

print(foo[1]) -- 1

for x=1, 3 do
     print(foo[x]) --1 2 3
end

local text = "foo"

print(foo[text]) --bar

print(foo.bar) --bar
I hope this gives you some inclination as to how to work with tables. If you wish to know more I recommend the lua users page on tables.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
User avatar
PedroChurro
Prole
Posts: 25
Joined: Tue Oct 09, 2012 8:22 pm
Location: In my room

Re: Tables for Dummies

Post by PedroChurro »

Lafolie wrote:A table is a fun, easy way to access and allocate memory. Tables are lua's greatest asset and cover the functionality of several constructs as used in other languages.

Most simply put, a table can be used as a list, array or dictionary (although there are many, many more way to use them!). We can think of these structures as containers for other things (variables), including more tables.

You declare a table like this:

Code: Select all

foo = {}
Now we can begin 'putting things into' our table. There are several ways to do this.

During the declaration:

Code: Select all

foo = {1, 2, "a", bar = "9001"}
With an element reference:

Code: Select all

foo[1] = "Hello!"
Using table.insert:

Code: Select all

table.insert(foo, "World!") --table.insert(table, value)
We can use all sorts of functions, loops and iterations to modify and play with our table, the simplest of which is the table.remove:

Code: Select all

table.remove(foo, 1) --remove element 1 from table 'foo'
When we need to reference something stored in a table we have many options also. Here's a simple table and some different ways to access it's elements:

Code: Select all

foo = {1, 2, 3, foo = "bar"}

print(foo[1]) -- 1

for x=1, 3 do
     print(foo[x]) --1 2 3
end

local text = "foo"

print(foo[text]) --bar

print(foo.bar) --bar
I hope this gives you some inclination as to how to work with tables. If you wish to know more I recommend the lua users page on tables.
Hmmm, thanks. I think I understand this now.
I try to make games.
spir
Citizen
Posts: 76
Joined: Wed Oct 17, 2012 1:12 pm

Re: Tables for Dummies

Post by spir »

PedroChurro wrote:Can someone explain me tables "for Dummies" style? Thanks!
I was about to propose starting a tutorial on lua tables and data structures, after noting many isssues on this forum related directly or indirectly to table usage (and meaning). I would start with my practices and views, then everyone could correct / improve / complete.

What do novices and other programmers think?

Denis

PS: One point is a common lack of overall programming knowledge does not help, in the very case of lua where every data struct is a table, to make fondamental distinctions between "object"-tables (records, tuples, forms, composite data in general) and collection-tables of all kinds; and for the latter between sequences/arrays, sets, ass. tables, thingies with nodes & links like graphs/trees/linked lists, matrices (maps of tiles)...
Another point is practicle usage, esp. that (precisely because of their generality), there is no builtin or even standard way to print out tables for the programmer's own feedback in debug, tests, maintenance... a constant annoyance if one cannot find personal ways to do that (and it's not trivial in the general case).
... la vita e estrany ...
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Tables for Dummies

Post by Roland_Yonaba »

That's a great idea, Denis. Well written tutorials are always welcome.
Anyway, I tend to think that most people don't actually take enough time to search.
Concerning tables, for instance, here are some great starting point:
Pil - Lua-Users Tables Tutorial - Gts Stolberg - Litt's Lab - MUSHClient tutorial
spir
Citizen
Posts: 76
Joined: Wed Oct 17, 2012 1:12 pm

Re: Tables for Dummies

Post by spir »

Roland_Yonaba wrote:That's a great idea, Denis. Well written tutorials are always welcome.
Anyway, I tend to think that most people don't actually take enough time to search.
Concerning tables, for instance, here are some great starting point:
Pil - Lua-Users Tables Tutorial - Gts Stolberg - Litt's Lab - MUSHClient tutorial
Thanks for the support. I will soon start.
About searching, you are right; but it is also often difficult to find the right doc matching one's level, programming knowledge, thinking style, etc... also many tutorials either confuse "novice" with stupid or their author seems unable to imagine what the blocking points may be for learners. Another point is, to my opinion, Lua and its coommunity are rather high-level programming; despite its original intent; because it's mainly used by game programmers, instead of players, who are touching at very complex programming tasks, including closer to the machine than usual programmers do, and most in collaboration with C.
Possibly a bit of collaboration, including form novice people who are in the best situation to explain others, may yield good results. The purpose is to have a one-stop place about tables and data structures to point people to when related issues arise in the Löve community (which does not prevent from also answering the precise question). (And if we are good, also from elsewhere.)

Denis

PS: IIRC, the tutorial in the Lua wiki is rather good. Maybe a starting place for some material. But (still IIRC) it does not really touch the basic point of what fondamentally different data structures tables may allow implementing, and above all what they mean, what they correspond to in a model to be expressed in code.
... la vita e estrany ...
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Tables for Dummies

Post by Roland_Yonaba »

spir wrote: Possibly a bit of collaboration, including form novice people who are in the best situation to explain others, may yield good results. The purpose is to have a one-stop place about tables and data structures to point people to when related issues arise in the Löve community (which does not prevent from also answering the precise question). (And if we are good, also from elsewhere.)
:awesome: :ultraglee:
spir
Citizen
Posts: 76
Joined: Wed Oct 17, 2012 1:12 pm

Re: Tables for Dummies

Post by spir »

... la vita e estrany ...
User avatar
Pixet Bits
Prole
Posts: 3
Joined: Fri Apr 26, 2019 6:29 pm

About Tables

Post by Pixet Bits »

what is the difference, or what is the correct way, to derive functions: use Tabe: functionName or Table.functionName?

Exemple :

```Group = {} ```
```Group.Function() ```
```end ```

```Group = {} ```
```Group:Function() ```
```end ```

[other question : how I create a topic of questions? I found a link in FAQ but i don't find the button "create a topic"]
MrFariator
Party member
Posts: 510
Joined: Wed Oct 05, 2016 11:53 am

Re: Tables for Dummies

Post by MrFariator »

The difference between a period and a colon is purely syntactical sugar. Consider the following:

Code: Select all

local myTable = {val = 0}

-- these two function declarations are functionally the same
function myTable.setVal ( self, val ) -- note that "self" here could be any other name as you wish
  self.val = val
end
function myTable:setVal ( val ) 
  self.val = val -- "self" is implicitly received by the function
end

-- these two lines are functionally the same
myTable.setVal ( myTable, 1 )
myTable:setVal ( 1 )
The colon syntax essentially automatically passes the table as the first argument to the function you are calling, and the function declared with a colon syntax can access it with "self". There is no functional difference between the two, so you may choose whichever feels more comfortable; there is no right or wrong.

As for your second question, the new topic button is can be found like this. Please, do not bump several year old threads in the future.
Post Reply

Who is online

Users browsing this forum: No registered users and 68 guests