Explaining Tables

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
baconhawka7x
Party member
Posts: 494
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Explaining Tables

Post by baconhawka7x »

I have been using tables in a lot of my tutorials, but have never really explained them in full detail to my viewers. So I am planning on filming a table-explanation video soon.

I was wondering if there was any specific thing(s) that some people might like me to share that they wish they would have known when they first learned what tables were.
User avatar
ivan
Party member
Posts: 1918
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Explaining Tables

Post by ivan »

When I first started using tables, I used tables strictly as "arrays".
So it would good to point out how to use tables to mimic a list, STL vector, queue or stack:

Code: Select all

local list = {}
table.insert(list, "a") -- push back
table.insert(list, 1, "b") -- push front
table.remove(list) -- pop back
table.remove(list, 1) -- pop front
size = #list -- get size
for i, v in ipairs(list) do end -- iterate
Some other Lua - specific tricks with tables include:

Code: Select all

list[1], list[2] = list[2], list[1] -- swap elements

list_copy = { unpack(list) } -- copy list table elements

-- removing multiple elements in a single pass
for i = #list, 1, -1 do
  if should_be_removed ( list[i] ) then
    table.remove(list, i)
  end
end
User avatar
riidom
Citizen
Posts: 74
Joined: Wed Jun 19, 2013 4:28 pm
Location: irgendwo an der Elbe
Contact:

Re: Explaining Tables

Post by riidom »

When strings start looking like a variable name:

Code: Select all

tbl.lalala == tbl["lalala"]
tbl.123 == tbl["123"] --nope

name = "Hans" age = 93
anOldGuy = Person{name = name, age = age} -- name = name??? Cool story, and 1+1=2 :D
--same as:
anOldGuy = Person:new()
anOldGuy.name = name
anOldGuy.age = age
--or
anOldGuy = Person:new()
anOldGuy["name"] = name
anOldGuy["age"] = age
From what I remember, these things made metrouble in the beginning.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Explaining Tables

Post by Roland_Yonaba »

@riidom: My advice, for what it's worth, would be not covering the OOP-like part. There is mo much to say about the raw use of tables,
so it might be better to dedicate an entire tutorial to the OOP topic.

@baconhawk7x:
The only one thing you start with: Lua arrays (lists) starts at 1. Not 0. :)

It would be nice to introduce Lua's table.library.

Also, you can cover the different way to loop through a table: numeric for (in normal and reverse ways), ipairs, pairs. Be sure to talk about the order in which they process the table key-element pairs.
You can also cover how one can get a table length (the count of elements) in a table using # operator (which is only valid for list-like arrays).
Maybe you can suggest a way to get that count in case the table is map-like:

Code: Select all

function count(t)
  local nvalues = 0
  for k,v in pairs(t) do nvalues = nvalues + 1 end
  return nvalues
end
You can also cover sparse arrays (what happens when you loop through them). Also, It'll be nice to explain how Lua deals with nils in table, and the consequence on iterators, too. The great Ivan made a nice suggestion, how to remove safely elements in table while looping through (loop in reverse).

You can also explain what happens when passing a table to print-like function. And, oh well, explain how one can serialize a flat/simple table. For complex cases (nested tables, cyclic references, there are serialization libraries).

Also, similar to what the gret Riidom suggested first, it will be nice to explain that almost anaything can be used as keys in tables: numbers, strings, functions, tables, userdata.

Also, something that lots of people get confused with, the assignment operator does not produce a reference free copy of a table.

Code: Select all

local t = {}
local a = t -- a and t are references to the same object.
print(a, t)
In case one wants a reference copy, you can introduce a simple deep_copy function for flat tables:

Code: Select all

local function flat_copy(t)
  local copy = {}
  for k,v in pairs(t) do copy[k] = v end
  return copy
end
After that, you can also introduce this new version which handles nested-arrays:

Code: Select all

local function nested_copy(t)
  local copy = {}
  for k,v in pairs(t) do
    if type(v) == 'table' then
      copy[k] = nested_copy(v)
    else
      copy[k] = v
    end
  end
  return copy
end
Last edited by Roland_Yonaba on Fri Jan 03, 2014 1:47 pm, edited 1 time in total.
User avatar
baconhawka7x
Party member
Posts: 494
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Re: Explaining Tables

Post by baconhawka7x »

Wow, thanks for all of the input!

I am planning on this being a more simplified tutorial, though. A lot of my tutorials utilize ipairs, without much explanation. So I was planning on kind of informing them about how that works and stuff. But I will be sure to implement as much of this stuff as I can!

Thanks:D
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Explaining Tables

Post by Roland_Yonaba »

Well, I was planning on writing a tutorial on tables. I pretty much copy-paste'd all of the randoms ideas I intended to cover ;)
User avatar
nuno
Party member
Posts: 137
Joined: Wed Dec 11, 2013 12:00 pm
Location: Portugal
Contact:

Re: Explaining Tables

Post by nuno »

please talk about changing/deleting elements during iteration. It seems many different problems can arise with different methods of doing it
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Explaining Tables

Post by micha »

One important thing for me is, that tables are handled by reference, so copying tables is difficult and sharing tables is easy.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests