Could someone go in debt for me about tables , It would really help me out because I don't seem to understand it from the wiki , thank you in advance it really helps me out .
Introduction
A table
is a data structure
fashioned for storing information for any practical use you find necessary. There are two forms
of tables: dictionaries
(also known as maps), and arrays
(also known as lists). These kinds of tables differ depending on how the content inside them is structured. Tables also hold their own individual place as a data type
(much like numbers, strings, booleans, ect, and how they're all individual data types as well).
How do you create a table?
A table is a set of two curly brackets { }
. Information that is in between those brackets, is the content in which the table stores. Just like every other data type, we can't just have a random table value floating around our script environment. We need to give it a name (specifically, a variable).
This is done by simply declaring your variable on the left, having your table value on the right, and obviously the equal sign in between:
-- Variable: "MyTable" -- Associated value: {} local MyTable = {}
Now we have a table value that we can reference
any time we want, without any syntax errors. You've successfully created a blank table.
Writing data to tables
Obviously the primary benefit of using tables is the ability to conveniently read
and write
data to it. There are multiple ways to do this, so I'll try to keep things in order. First, let's create a table with a set of information given to it first-hand:
local MyTable = {1} -- This is a table, with the number '1' stored as it's first index.
To assign multiple values, simply add either a coma ,
or semicolon ;
. Whichever you want to use, is completely up to you. Let's try it:
local MyTable = {1,2,3,4,5} -- Numbers 1-5 stored in the table -- Or you could say... local MyTable = {1;2;3;4;5} -- Using semicolons. Same thing.
The wonderful thing about tables, is that it's not limited to what kind of values can be stored inside of it. You can use it as a container to hold, just about anything you want. Including physical objects in your game! Here's an example:
local MyTable = {"string value",workspace.Part,64,true,false,{}} -- You can even store more tables inside tables!
Formatting your table
Just for future reference, the way you format your table is also up to you. Some people keep their entire table on one line, others will span it out like this:
local MyTable = { "string value", 100, 64, true, false, {}, }
Either way is fine. Personally, I find the format of the example above a bit more clear to beginners, so they can see how tables work in more of a physical "list" form.
Reading data from tables
The term index
is commonly used to refer to a value's position, or key (variable) in a table. For example, if I had a table with 5 string values in it like this:
local MyTable = { "i", -- This value's index is 1, since it's the first value in the table. "am", -- This value's index is 2, since it's the second "a", -- This has an index of 3 "string", -- An index of 4 "sequence" -- And you guessed it...5 }
This "index" is how we read data from tables. So, if we wanted to print the string value "am", from the example above, we'd simply request the second index, which corresponds to it's value ("am"). We can index a table in one of two ways: using a dot .
, or using square brackets [ ]
. This is how we tell the script we're trying to look inside of something. Here's an example continuing the demonstration above:
-- Since it's a syntax error to index something for a number, or anything that's not a character in the alphabet (besides underscores), we can't say something like this: print(MyTable.2) -- This is a syntax error -- Instead, we'll need to use square brackets, then put our index number in there. print(MyTable[2]) -- Perfect! This will index the table for it's second value. In this case, it should be a string value called "am".
Square brackets are used to index a table for something that goes against the limitations of what variables can be. Go here to better understand how variables work: http://wiki.roblox.com/index.php?title=Variable#Wait
You can even make variables that save information from a table, by making it equal it's index. Like so:
local MyTable = {"cookies","mangos"} -- Table local SavedIndex = MyTable[1] -- Saving "cookies" to the "SavedIndex" variable. print(SavedIndex) -- Printing what "SavedIndex" represents, which will be the string value "cookies".
Table manipulation
There are all kinds of crazy built-in functions you can use to mess with tables inside the table
library. I'm not going to cover them in this explanation, but you should definitely check them out: http://wiki.roblox.com/index.php?title=Function_dump/Table_manipulation#table.foreach_.28table.2C_f.29
Inserting data to tables
Just as we can put some "default" values in tables, one of the biggest uses of them is the ability to continue writing data to the table, even after it's set. To do this, we first index the table for the position, or variable we're looking to change, then proceed changing it just as if we're creating any ordinary variable. Here's an example:
local MyTable = {} -- An empty table print(MyTable[1]) -- This will print nil. There is no value associated with the table's first index! -- Now let's say I want to insert a value into it's first index... MyTable[1] = "first value!" -- The same way we indexed the table before, we do again while assigning it a value. Now the string "first string!" holds the first position in the table, with an index of 1. print(MyTable[1]) -- Aha, now we have a value associated with the first index. This will print "first value!"
Arrays
Now that we've covered all those basics, it's time to get into the difference between dictionaries
and arrays
- and the difference is really quite simple.
If I where to sum up the difference between them in just once sentence, it'd be: arrays are tables with ONLY numeric keys
(or, indices), and a dictionary is a table with ONLY non-numeric keys.
For example, the demonstrations I've been showing you from above, are all examples of arrays
, because they all have an index that's a number
(the value's position in the table).
If you wanted a physical representation of what an array looks like, this would be a good chart:
-- What an array really looks like: local MyTable = { [1] = "i", [2] = "am", [3] = "a", [4] = "string", [5] = "sequence" }
And you actually can set up arrays like this. The only difference between this, and what I've shown you before, is simply that we don't see the index numbers when creating a "normal" table (but they are there, that's why we index arrays with numbers).
Dictionaries
Dictionaries
are the same format as an array (in the sense of having a key and a value
), however, the keys dictionaries have are not number values. If there is but one index in a dictionary that's a number, it is not considered a dictionary anymore, but rather a mix between a dictionary and an array (which you should avoid).
Dictionaries also bring the "variable" concept into play, which I was hinting at before. Dictionaries give us the ability to assign custom keys to a table, with a corresponding value (just like regular variables!)
Here's an example:
-- This is a dictionary formatted table. local MyTable = { x = 'hello', -- Here we overwrite the 1st index, to 'x' y = 'world', -- Here we overwrite the 2nd index, to 'y' z = 'apples', -- And here we overwrite the 3rd index, to 'z' }
Likewise with indexing dictionaries, we use their defined keys to get the values they're associated with. And in this case, we don't need to use brackets! We can say something like this:
print(MyTable.x) -- prints 'hello' print(MyTable.y) -- prints 'world' print(MyTable.z) -- prints 'apples'
Summary, Information, and help
I'd write more, but I'm afraid to surpass the character limit of the answer. You could consider this part 1 to learning about tables, and anything else you need help with regarding them I'd be glad to assist with.
I'll be making some tutorials on my YouTube channel
later, some involving how tables work. If you wanna look out for that, search "ScriptGuider
" on YouTube.
Good luck, and hope it helped!