This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
AnthonyD98™'s profile picture

Could someone explain Tables to me?

Started by AnthonyD98™, 28 January 2013 - 10:34 AM
AnthonyD98™ #1
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
grand_mind1 #2
Posted 28 January 2013 - 11:44 AM
I learned how to use tables from direwolf20 in this video:
http://www.youtube.com/watch?v=1YMplg5ihYI
Not sure if it will help you, but it did for me.
NeverCast #3
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!
AnthonyD98™ #4
Posted 28 January 2013 - 12:11 PM
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!
NeverCast #5
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.
TheOddByte #6
Posted 30 January 2013 - 09:59 AM
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/>
AnthonyD98™ #7
Posted 02 February 2013 - 04:38 PM
How would I write a table to a file?
Kingdaro #8
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()
Jhaymes #9
Posted 02 February 2013 - 11:20 PM
Registered just to say hello to fellow New Zealanders! From quake-city here.