So you want to learn how to manage large amounts of info and store/recall it at any time, essentially you are looking for a database.
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 table
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 variables
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 print
1="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
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 />
You can certainly access a table with a variable, if you use the mytable[variable] syntax rather than the mytable.string form.
Defining the table and then populating it later is perfectly acceptable, as in:
mytable={}
mytable[1]="first value"
mytable[2]="second value"
You can both set and access the stored values later on using variables, even contained in the same table:
mytable[3]=2
print(mytable[mytable[3]])
outputs 'second value'
Of course, at some point doing that sort of thing can become pointlessly confusing, but using such methods can also substantially improve the functionality of your programs. Where the line is depends on the audience, I suppose.
you sould be able to set an empty table's metatable to make saving to a file easier
__index will be executed when its trying to get a nil value, thats why the table needs to be empty
__newindex will be executed when you try to create a new index, and sence its empty it will be called every time
You can certainly access a table with a variable, if you use the mytable[variable] syntax rather than the mytable.string form.
Defining the table and then populating it later is perfectly acceptable, as in:
mytable={}
mytable[1]="first value"
mytable[2]="second value"
You can both set and access the stored values later on using variables, even contained in the same table:
mytable[3]=2
print(mytable[mytable[3]])
outputs 'second value'
Of course, at some point doing that sort of thing can become pointlessly confusing, but using such methods can also substantially improve the functionality of your programs. Where the line is depends on the audience, I suppose.
That's what I am saying, you do it like that, it is unfortunate that you cannot use a variable with the table.delimiter format, you can with the table[delimeter] format