Difference between revisions of "Tables"

From ComputerCraft Wiki
Jump to: navigation, search
(TgAHzUUCMhuL)
(Undo revision 2172 by 212.142.46.129 (talk))
Line 1: Line 1:
we bought the south shore saaanvh changing table because it was not expensive and fit into our apt. It was delivered promptly nicely packaged. I am not handy and I knew that this would take me a long time to build but when i opened the box and saw the amount of parts i flipped, to make matters worse the instructions were so hard to figure out. I ended up hiring a handyman and it took him 2 hours and cost me $50. But after all that it was worth it. The changing table is beautiful and very sturdy it looks like a $400 piece. If your looking for a great changing table at a great price this is your best bet. (just be aware its frustrating to build)
+
[[Category:Tutorials]]
 +
This script will introduce you to tables, and show you some ways to use them.
 +
If you have experience with other languages, a table is roughly equivalent to an array.
 +
 
 +
Wow! Great to find a post with such a clear maesgse!
 +
 
 +
== Using the table ==
 +
 
 +
There are a number of ways to set the values in a table. The first, as shown above, is the simplest method, and will give each value a unique number as the "key". The key is simply the location in the table, it's usually a number or string. This method always clears the contents of the table and replaces it with what you've specified. Note that setting a table to "{}" is valid, and is commonly used if the values don't need to, or can't, be set immediately. You may find yourself using "Variable={}" often. Setting a specific key with this method is relatively simple, you just use the asignment operator "=" and treat the key as a variable
 +
local Table = {Key1 = "First key!", Key2 = "Second key!"}
 +
 
 +
It can also be defined over multiple lines, to help with readability, if you end the line with a comma rather than the } to close it. Just remember to add the } when you're done.
 +
local Table = {"Stuff", 1, 2, "More stuff",
 +
    3, "Some strings",
 +
    function() end}
 +
 
 +
Another method is to use brackets, [ and ], to specify the key. This method requires the table to have already been created, using { and }, otherwise it will give you an error. Using this method will treat it as a regular variable, so you can both set and read the value with this method. This method is commonly used for getting a value when the key is a number.
 +
local Table = {}
 +
Table["Boolean"] = true
 +
 +
if Table["Boolean"] then print("The value is true!") end
 +
 
 +
If you need to reference a table within a table using this method, you can simply add another [ ]
 +
 
 +
local Table = {"Value", 2, {true, false}}
 +
 +
if Table[3][2] then print("The value is true!") else print("The value is false!") end
 +
 
 +
A third method is to simply add the key onto the end of the table name, separated by a ".". Like the previous method, this method requires the table to already exist. It can also be treated as a regular variable, as above. This method is commonly used for when the key is a string.
 +
local Table = {}
 +
 +
Table.key = true
 +
if Table.key then print("True!") end
 +
 
 +
As with the second method, to index a table within a table you just add the second key to the end. Note that these methods are interchangeable, and can all be used at the same time.
 +
 
 +
It is not advisable to have many tables inside each other, especially if they are similarly named
 +
local Table = {String = "This is a string!", Table = {Number = 2, Table = {func = write, Table = {Bool = true} } }}
 +
 +
if Table[Table].Table.Table.Bool then print("True!") end
 +
 
 +
If you don't mind being restricted to number keys, you could use the table.insert(Table, [Position,] Value ) function.
 +
local Table = {"One"}
 +
table.insert(Table, "Three") --This inserts the string "Three" to the end of the table (At key 2)
 +
table.insert(Table, 2, "Two") --This inserts the string "Two" to key 2, pushing the string "Three" up to key three
 +
--End result, Table[1] is One, Table[2] is Two, Table[3] is Three
 +
 
 +
== Tables and Loops ==
 +
 
 +
When you combine tables and loops, they suddenly become a lot more useful. You can run through a table easily, using or changing each value.
 +
local Table = {"Hello! ", "This string ", "was read from ", "a table!", "\n"}
 +
for i=1,#Table do
 +
    write( Table[i] )
 +
end
 +
This will write "Hello! This string was read from a table!" followed by a new line. You may notice there's a "#" behind Table, this simply returns the number of values in the table. It can also be used to get the length of a string.
 +
 
 +
The standard for loop does, however, come with an inherent disadvantage in that it can only (easily) access consecutive numerical strings, ie 1,2,3 but not 1,2,4. to get around this, we can use "pairs" and "ipairs". "pairs" and "ipairs" use a table to loop, by running through the keys and values for the number that exist in the table.
 +
local Table = {StartTime = 0.5, EndTime = 1.5}
 +
 +
for key,value in pairs( Table ) do
 +
    print( tostring(key) ..": "..tostring(value))
 +
end
 +
This will output equivalent to "EndTime: 1.5\nStartTime: 0.5". Note that ipairs is identical to pairs, except it tries to do it in order.

Revision as of 13:12, 17 July 2012

This script will introduce you to tables, and show you some ways to use them. If you have experience with other languages, a table is roughly equivalent to an array.

Wow! Great to find a post with such a clear maesgse!

Using the table

There are a number of ways to set the values in a table. The first, as shown above, is the simplest method, and will give each value a unique number as the "key". The key is simply the location in the table, it's usually a number or string. This method always clears the contents of the table and replaces it with what you've specified. Note that setting a table to "{}" is valid, and is commonly used if the values don't need to, or can't, be set immediately. You may find yourself using "Variable={}" often. Setting a specific key with this method is relatively simple, you just use the asignment operator "=" and treat the key as a variable

local Table = {Key1 = "First key!", Key2 = "Second key!"}

It can also be defined over multiple lines, to help with readability, if you end the line with a comma rather than the } to close it. Just remember to add the } when you're done.

local Table = {"Stuff", 1, 2, "More stuff",
    3, "Some strings",
    function() end}

Another method is to use brackets, [ and ], to specify the key. This method requires the table to have already been created, using { and }, otherwise it will give you an error. Using this method will treat it as a regular variable, so you can both set and read the value with this method. This method is commonly used for getting a value when the key is a number.

local Table = {}
Table["Boolean"] = true

if Table["Boolean"] then print("The value is true!") end

If you need to reference a table within a table using this method, you can simply add another [ ]

local Table = {"Value", 2, {true, false}}

if Table[3][2] then print("The value is true!") else print("The value is false!") end

A third method is to simply add the key onto the end of the table name, separated by a ".". Like the previous method, this method requires the table to already exist. It can also be treated as a regular variable, as above. This method is commonly used for when the key is a string.

local Table = {}

Table.key = true
if Table.key then print("True!") end

As with the second method, to index a table within a table you just add the second key to the end. Note that these methods are interchangeable, and can all be used at the same time.

It is not advisable to have many tables inside each other, especially if they are similarly named

local Table = {String = "This is a string!", Table = {Number = 2, Table = {func = write, Table = {Bool = true} } }}

if Table[Table].Table.Table.Bool then print("True!") end

If you don't mind being restricted to number keys, you could use the table.insert(Table, [Position,] Value ) function.

local Table = {"One"}
table.insert(Table, "Three") --This inserts the string "Three" to the end of the table (At key 2)
table.insert(Table, 2, "Two") --This inserts the string "Two" to key 2, pushing the string "Three" up to key three
--End result, Table[1] is One, Table[2] is Two, Table[3] is Three

Tables and Loops

When you combine tables and loops, they suddenly become a lot more useful. You can run through a table easily, using or changing each value.

local Table = {"Hello! ", "This string ", "was read from ", "a table!", "\n"}
for i=1,#Table do
   write( Table[i] )
end

This will write "Hello! This string was read from a table!" followed by a new line. You may notice there's a "#" behind Table, this simply returns the number of values in the table. It can also be used to get the length of a string.

The standard for loop does, however, come with an inherent disadvantage in that it can only (easily) access consecutive numerical strings, ie 1,2,3 but not 1,2,4. to get around this, we can use "pairs" and "ipairs". "pairs" and "ipairs" use a table to loop, by running through the keys and values for the number that exist in the table.

local Table = {StartTime = 0.5, EndTime = 1.5}

for key,value in pairs( Table ) do
   print( tostring(key) ..": "..tostring(value))
end

This will output equivalent to "EndTime: 1.5\nStartTime: 0.5". Note that ipairs is identical to pairs, except it tries to do it in order.