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

Storing information effectively

Started by KaoS, 03 August 2012 - 11:09 AM
KaoS #1
Posted 03 August 2012 - 01:09 PM
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
Spoilera table can be created with the same syntax as creating a variable except that the value is surrounded with '{' &amp; '}'<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

add 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 &amp; 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

add 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 one

if 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
Spoilerone 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


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:/>/>
SavinaRoja #2
Posted 03 August 2012 - 01:46 PM
When I make a table with keyword-value pairs in creation, I have to use the following syntax.
mytable = { [1] = "first value", ["custom"] = "second value" }
Brackets must surround the keyword/keynumber I believe.
KaoS #3
Posted 03 August 2012 - 01:52 PM
ah yes, thanks
SavinaRoja #4
Posted 03 August 2012 - 02:00 PM
This is a good and concise explanation of tables and the serialize/unserialize tools. I wish I had found this a couple weeks ago. Good work.
Xfel #5
Posted 03 August 2012 - 02:28 PM
You should mention that

mytable.something
is equal to

mytable["something"]
KaoS #6
Posted 03 August 2012 - 03:31 PM
yeah, was planning to, sorry about that, unfortunate that you cannot use a variable there
ChunLing #7
Posted 03 August 2012 - 09:42 PM
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.
PixelToast #8
Posted 03 August 2012 - 09:54 PM
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
KaoS #9
Posted 06 August 2012 - 09:03 AM
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