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.
Explaining Tables
- baconhawka7x
- Party member
- Posts: 494
- Joined: Mon Nov 21, 2011 7:05 am
- Location: Oregon, USA
- Contact:
Re: Explaining Tables
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:
Some other Lua - specific tricks with tables include:
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
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
Re: Explaining Tables
When strings start looking like a variable name:
From what I remember, these things made metrouble in the beginning.
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
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: Explaining Tables
@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:
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.
In case one wants a reference copy, you can introduce a simple deep_copy function for flat tables:
After that, you can also introduce this new version which handles nested-arrays:
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 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)
Code: Select all
local function flat_copy(t)
local copy = {}
for k,v in pairs(t) do copy[k] = v end
return copy
end
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.
- baconhawka7x
- Party member
- Posts: 494
- Joined: Mon Nov 21, 2011 7:05 am
- Location: Oregon, USA
- Contact:
Re: Explaining Tables
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
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
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: Explaining Tables
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
Re: Explaining Tables
please talk about changing/deleting elements during iteration. It seems many different problems can arise with different methods of doing it
Re: Explaining Tables
One important thing for me is, that tables are handled by reference, so copying tables is difficult and sharing tables is easy.
Check out my blog on gamedev
Who is online
Users browsing this forum: No registered users and 2 guests