the secret to effective information storage is the table, it allows you to store infinite amounts of information under on name
basic table use
Spoiler
a table can be created with the same syntax as creating a variable except that the value is surrounded with '{' & '}'<br>you can leave the space between them empty and it will make an empty table:mytable={}
you can make an empty table and then add values or add them as you create the tableadd later
Spoiler
mytable={}
mytable[1]="first value"
mytable[2]="second value"
print(mytable[2])
with this method you can place anything between the [] and it will be used to assign it any defining value (the 1 & 2 or anything in the [] is the defining value), note that strings must still be surrounded with inverted commas or they will be treated as variablesadd in creation
Spoiler
mytable={"first value", "second value"}
print(mytable[2])
when you add items separated by commas then it assigns them numerical defining values starting from oneif you want to use a custom defining value you must set it as though you were setting a variable within the {} demarcation
mytable={[1]="first value", ["custom"]="second value"}
print(mytable["custom"])
you recall items from a table in the same way you add them, with []
print(mytable[3])
or
print(mytable.3)
a table can be added to and edited at any time, you can store anything in it as well: strings, numbers and even functions
here is a more advanced way of using tables
Spoiler
one of the most incredibly useful attributes of a table is that you can put a table in a table
mytable={}
mytable["secondary"]={}
at this stage all we have is a way to store info, the table becomes an effective method when you use variables within it to access different parts of the table and pull values or set values
mytable={"first value", "second value", "third value", "fourth value", "fifth value", "sixth value", "seventh value"}
print("which value would you like")
number=tonumber(read())
print("nThat value is "..mytable[number])
once you have gotten used to that you can use a secondary table to store a list of usernames or computer ids etc, making each a table of its own and then give that table values as notes about it. for example if you have a radar you can use it to store the computers detected like this
mytable={}
mytable["positions"]={}
--when you detect a computer return the variables 'detectedid' as the ID of the computer/turtle the radar found, 'x', 'y', 'z' as that computer/turtle's location in co-ordinate form and 'msg' as the message it was sending that was detected by the radar
mytable["positions"][detectedid]={}
mytable["positions"][detectedid]["position"]={}
mytable["positions"][detectedid]["position"]["x"]=x
mytable["positions"][detectedid]["position"]["y"]=y
mytable["positions"][detectedid]["position"]["z"]=z
mytable["positions"][detectedid]["message"]=msg
so all of the data is stored in a table, you can then recall all data from a table with the pairs() command
mytable={"first value", "second value", "third value", "fourth value", "fifth value", "sixth value", "seventh value"}
for define, value in pairs(mytable) do
print(define.."="..value)
end
would go through the entire table and print1="first value"
2="second value"
3="third value"
etc
so now we can create/edit values and effectively retrieve information from the table but if the computer is reset all of that information is lost and you cannot write a table in its default form to a file for later loading, this is where the textutils api comes in
textutils.serialize(mytable) returns the table 'mytable' in string form
mytable={}
mytable[1]="first value"
mytable[2]="second value"
print(textutils.serialize(mytable))
would return '{1="first value", 2="second value"}' as a string so it can then be written to a file, assigned to a variable or even sent over rednet to another computer, there is a big catch to this though, this combines all values into one string so everything in the table must be able to be converted into a string. this eliminates functions (but you can still store them as a string and then use loadstring() to convert it back)then you use textutils.unserialize(storedtable) to convert it back into a table once you have read the file or received the message so we can now send large amounts of information in a single message
using this method we can store a large amount of information in a file and read it back without having to make a code go through the file line by line assigning variables, the table makes retrieving information much easier as you can use variables as the defining value and you do not clutter up your system with variables (be careful though or you will clutter up your tables)
if you have any questions PM me or post here, I hope I have been clear and this is understandable, good luck :ph34r:/>/>