Jump to content




Could someone explain Tables to me?


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

#1 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 28 January 2013 - 11:34 AM

Hello I have a question:

Could someone explain tables to me?

- How do they work?
- What do they do?
- How can I use them? (Example codes)
- How they make my life eaiser ?
- How to use them ?

Thanks, AnthonyD98

#2 grand_mind1

  • Members
  • 207 posts

Posted 28 January 2013 - 11:44 AM

I learned how to use tables from direwolf20 in this video:

Not sure if it will help you, but it did for me.

#3 NeverCast

  • Members
  • 400 posts
  • LocationChristchurch, New Zealand

Posted 28 January 2013 - 11:58 AM

Tables are dictionaries/ arrays and everything that is awesome in programming, all rolled in to one!
Tables are basically a key value map. they connect a key, with a value. A key can be anything and the value can be anything. Including another table.

To create an empty table, just go like this

myTable = {}
Now we have an empty table.

If we wanted the table to remember our name, we could go like this
myTable["name"] = "NeverCast"

Now the table has linked 'name' with 'NeverCast'. If we wanted to recall that later we can go like this.

print("Hello I am ".. myTable["name"])
This would print out Hello I am NeverCast .... Nice to meet you btw!

Tables can also be used as an array, If you've used arrays before, you know they store values and index them by a number.
In Lua, Arrays start at the number 1 ( not 0 like languages such as Java and C# )
If we wanted to easily add a value to a table we can use, table insert

table.insert(myTable, "hello world!")
Now 1 in the table has the value "hello world!"
We can recall it like this:

helloMessage = myTable[1]

We can also see how many items are in a table by using a # before the table.
( Note: It only counts sequentially, 1,2,3 etc. It does not count us putting 'name' in to the table )
print(#myTable)

That will print 1, as we added hello world!, Note how it does not say two even though 'name' is stored in there? That's because it only counts numbers as an array.

As you can see tables can be used for all sorts of things. You can also index them by using a dot. Like this!
print(myTable.name)

Hope this helps get you started with tables! There are lots of tutorials on the internet that will be more helpful than me!

#4 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 28 January 2013 - 12:11 PM

View PostNeverCast, on 28 January 2013 - 11:58 AM, said:

Tables are dictionaries/ arrays and everything that is awesome in programming, all rolled in to one!
Tables are basically a key value map. they connect a key, with a value. A key can be anything and the value can be anything. Including another table.

To create an empty table, just go like this

myTable = {}
Now we have an empty table.

If we wanted the table to remember our name, we could go like this
myTable["name"] = "NeverCast"

Now the table has linked 'name' with 'NeverCast'. If we wanted to recall that later we can go like this.

print("Hello I am ".. myTable["name"])
This would print out Hello I am NeverCast .... Nice to meet you btw!

Tables can also be used as an array, If you've used arrays before, you know they store values and index them by a number.
In Lua, Arrays start at the number 1 ( not 0 like languages such as Java and C# )
If we wanted to easily add a value to a table we can use, table insert

table.insert(myTable, "hello world!")
Now 1 in the table has the value "hello world!"
We can recall it like this:

helloMessage = myTable[1]

We can also see how many items are in a table by using a # before the table.
( Note: It only counts sequentially, 1,2,3 etc. It does not count us putting 'name' in to the table )
print(#myTable)

That will print 1, as we added hello world!, Note how it does not say two even though 'name' is stored in there? That's because it only counts numbers as an array.

As you can see tables can be used for all sorts of things. You can also index them by using a dot. Like this!
print(myTable.name)

Hope this helps get you started with tables! There are lots of tutorials on the internet that will be more helpful than me!

Awesome tutorial thank you!

#5 NeverCast

  • Members
  • 400 posts
  • LocationChristchurch, New Zealand

Posted 28 January 2013 - 12:15 PM

You're very welcome.

remember that arrays are just emulated using numbers as the key, so you can still go myTable[1] = value etc.
Also tables can take afaik, anything as a key, and anything as a value. So you can go myTable[myTable] = myTable
Now you can access your table through your table using your table as a key :P

myTable = myTable[myTable] 

But you wouldn't, cause that's crazy :D
Also when using myTable.someKey, It basically changes that to myTable["someKey"] at runtime. So you can only use strings in this dotting manner.

#6 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 30 January 2013 - 09:59 AM

View PostNeverCast, on 28 January 2013 - 12:15 PM, said:

You're very welcome.

remember that arrays are just emulated using numbers as the key, so you can still go myTable[1] = value etc.
Also tables can take afaik, anything as a key, and anything as a value. So you can go myTable[myTable] = myTable
Now you can access your table through your table using your table as a key :P

myTable = myTable[myTable] 

But you wouldn't, cause that's crazy :D
Also when using myTable.someKey, It basically changes that to myTable["someKey"] at runtime. So you can only use strings in this dotting manner.
Now I Really got dizzy when you talked about a table in a table and so on.. :P
Great Tutorial Anyway! :D

#7 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 02 February 2013 - 04:38 PM

How would I write a table to a file?

#8 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 02 February 2013 - 05:22 PM

You can turn tables into a string using textutils.serialize.
local strtable = textutils.serialize(sometable)

Then write that string to a file.
local file = fs.open('somefilepath','w')
file.write(strtable)
file.close()


#9 Jhaymes

  • New Members
  • 1 posts

Posted 02 February 2013 - 11:20 PM

Registered just to say hello to fellow New Zealanders! From quake-city here.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users