Jump to content




How To Use Tables.


  • You cannot reply to this topic
37 replies to this topic

#1 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 29 October 2013 - 08:41 AM

How to use tables

(and use them well)


Welcome to this tutorial about using tables. This tutorial is split into parts:

Part I - Creating a table

Part II - Numbers as keys

Part III - Strings as keys

Part IV - Initializing a table with data already in it

Part V - Using tables

Thank you for reading this tutorial! Please point out any mistakes, and advise me on using tables even better! :)/>/>

-Z

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 29 October 2013 - 10:18 AM

There is a lot that you've missed. You haven't pointed out anything such as mixing indexed and key/value elements, negative indexes, iterating through tables, both with a numerical for loop and a generic for loop (pairs/ipairs). The advantages of each type of loop (incremental/ipairs/pairs), especially when dealing with custom indexes such as a table that only contains indexes 1,2,4,17,700. There are various other improvements along these lines that you could make.

I think that maybe explaining and/or showing an example of how tables can help us capture all return results from a function call, or all arguments in a vararg function.

You also don't state why it is better (efficiency-wise and memory-wise) to define the elements in a table when invoking the table constructor. Which I think is needed rather than just stating "but it is quicker and shorter to do this"

Also consider using locals in all your code examples in an attempt to promote good coding habits.

#3 sens

  • Members
  • 49 posts
  • LocationFrance

Posted 29 October 2013 - 10:33 AM

View Posttheoriginalbit, on 29 October 2013 - 10:18 AM, said:

You also don't state why it is better (efficiency-wise and memory-wise) to define the elements in a table when invoking the table constructor. Which I think is needed rather than just stating "but it is quicker and shorter to do this"
I'm relatively new to Lua and may be mistaken about this, but what I've read seems to indicate that the two are completely equivalent:

Programming in Lua, first edition said:

That is, all tables are created equal; constructors only affect their initialization.
http://www.lua.org/pil/3.6.html

#4 Agoldfish

  • Members
  • 451 posts
  • LocationSome Fish Bowl in Ohio.

Posted 29 October 2013 - 11:01 AM

It seems like you made this after you told me how to use tables :3. Anyway, nice tutorial, might come in handy sometime.

#5 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 29 October 2013 - 05:39 PM

Good tutorial, but:
local tbl = {["foo"]="bar"}

-------------------------
--This
print(tbl["foo"])
--Is not the same as
print(tbl.foo)
-------------------------
--To do the second type you have to do it like this
local tbl = {foo = "bar"}
print(tbl.foo)
:D

#6 MudkipTheEpic

  • Members
  • 639 posts
  • LocationWhere you'd least expect it.

Posted 29 October 2013 - 05:45 PM

View PostFreack100, on 29 October 2013 - 05:39 PM, said:

Good tutorial, but:
local tbl = {["foo"]="bar"}

-------------------------
--This
print(tbl["foo"])
--Is not the same as
print(tbl.foo)
-------------------------
--To do the second type you have to do it like this
local tbl = {foo = "bar"}
print(tbl.foo)
:D

Misinformation.

It works either way.

#7 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 29 October 2013 - 07:31 PM

View PostMudkipTheEpic, on 29 October 2013 - 05:45 PM, said:

View PostFreack100, on 29 October 2013 - 05:39 PM, said:

Good tutorial, but:
local tbl = {["foo"]="bar"}

-------------------------
--This
print(tbl["foo"])
--Is not the same as
print(tbl.foo)
-------------------------
--To do the second type you have to do it like this
local tbl = {foo = "bar"}
print(tbl.foo)
:D/>

Misinformation.

It works either way.
o.O
Since when?
I never saw it...

Sorry for the missinformation BTW...

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 29 October 2013 - 07:40 PM

View PostFreack100, on 29 October 2013 - 07:31 PM, said:

o.O
Since when?
I never saw it...
Since always. The dot notation is just syntactical sugar. The only time it doesn't work is in this case
local t = {["some key"] = true}
print(t.some key) --# this doesn't work, just like the next line doesn't work
t = {some key = true} --# they follow the same rule pattern


#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 29 October 2013 - 07:41 PM

View Posttheoriginalbit, on 29 October 2013 - 07:40 PM, said:

View PostFreack100, on 29 October 2013 - 07:31 PM, said:

o.O
Since when?
I never saw it...
Since always. The dot notation is just syntactical sugar. The only time it doesn't work is in this case
local t = {["some key"] = true}
print(t.some key) --# this doesn't work, just like the next line doesn't work
t = {some key = true} --# they follow the same rule pattern

What you're thinking is that THESE aren't the same
local var = "hello"
local t = {}
t[var] = true --# adds true under the key "hello"
print(t.var) --# looks for a key "var"
which they are not the same.

#10 Symmetryc

  • Members
  • 434 posts

Posted 29 October 2013 - 08:11 PM

You can actually use almost any value as a key in a table, not just strings and numbers, so this will work:
myTable[{}] = 5
myTable[true] = 10


#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 29 October 2013 - 08:15 PM

View PostSymmetryc, on 29 October 2013 - 08:11 PM, said:

You can actually use almost any value as a key in a table, not just strings and numbers, so this will work:
myTable[{}] = 5
Of course hoping that you save the table so that you can access it later!
local k = {}
local t = {[k] = "tables as keys!"}
print(t[k])


#12 Symmetryc

  • Members
  • 434 posts

Posted 29 October 2013 - 08:22 PM

View Posttheoriginalbit, on 29 October 2013 - 08:15 PM, said:

View PostSymmetryc, on 29 October 2013 - 08:11 PM, said:

You can actually use almost any value as a key in a table, not just strings and numbers, so this will work:
myTable[{}] = 5
Of course hoping that you save the table so that you can access it later!
local k = {}
local t = {[k] = "tables as keys!"}
print(t[k])
Well, it depends. Let's say you wanted to just make a table of information that your going to return (you're doing this in a function), the end user will use pairs/next to get the values, so there is really no functional purpose in saving them, although it does add to the readability (*wink* :P *wink*).

#13 MudkipTheEpic

  • Members
  • 639 posts
  • LocationWhere you'd least expect it.

Posted 29 October 2013 - 09:37 PM

View PostFreack100, on 29 October 2013 - 07:31 PM, said:

-snip-
Sorry for the misinformation BTW...

It's OK, everyone makes mistakes.

#14 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 30 October 2013 - 02:13 AM

View PostSymmetryc, on 29 October 2013 - 08:11 PM, said:

You can actually use almost any value as a key in a table, not just strings and numbers, so this will work:
myTable[{}] = 5
myTable[true] = 10

I put that in the first part.

#15 Symmetryc

  • Members
  • 434 posts

Posted 30 October 2013 - 01:52 PM

View PostZudoHackz, on 30 October 2013 - 02:13 AM, said:

View PostSymmetryc, on 29 October 2013 - 08:11 PM, said:

You can actually use almost any value as a key in a table, not just strings and numbers, so this will work:
myTable[{}] = 5
myTable[true] = 10

Anyway, it is really stupid to use boolean and tables as well.
I hope you were kidding.

#16 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 31 October 2013 - 12:46 AM

View PostZudoHackz, on 30 October 2013 - 02:13 AM, said:

I put that in the first part. Anyway, it is really stupid to use boolean and tables as well.
Man, that hurt... That was like saying it's stupid to use C++. You can't just go over and state that a bunch of people is stupid, just because they do things you don't.

#17 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 31 October 2013 - 04:00 AM

View PostLBPHacker, on 31 October 2013 - 12:46 AM, said:

View PostZudoHackz, on 30 October 2013 - 02:13 AM, said:

I put that in the first part. Anyway, it is really stupid to use boolean and tables as well.
Man, that hurt... That was like saying it's stupid to use C++. You can't just go over and state that a bunch of people is stupid, just because they do things you don't.

I'm not saying that people are stupid, I was just saying that using boolean and tables is a bad idea, unless there is actually a good reason for doing it.

#18 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 31 October 2013 - 07:08 AM

You should also explain what happens when you do the following:
local t = {
  x = "random",
  s = "key"
}

local x = t

A hint what you should explain is actually this "strange" behaviour:
x.lol = ":D/>"
print( t.lol )

If you dont understand whats going on there, why have you written this tutorial? 0.o

#19 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 02 November 2013 - 02:25 PM

What Engineer demonstrated has actually screwed me up quite a bit when I was developing with LÖVE a while ago, so it'd be good for OP to explain that.

#20 ChemE

  • Members
  • 4 posts

Posted 20 November 2013 - 10:53 AM

Nice tutorial thanks!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users