Jump to content




Storing information effectively


  • You cannot reply to this topic
8 replies to this topic

#1 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 03 August 2012 - 12: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
Spoiler

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

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:/>

#2 SavinaRoja

  • New Members
  • 15 posts

Posted 03 August 2012 - 12: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.

#3 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 03 August 2012 - 12:52 PM

ah yes, thanks

#4 SavinaRoja

  • New Members
  • 15 posts

Posted 03 August 2012 - 01: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.

#5 Xfel

    Peripheral Designer

  • Members
  • 515 posts

Posted 03 August 2012 - 01:28 PM

You should mention that
mytable.something
is equal to
mytable["something"]


#6 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 03 August 2012 - 02:31 PM

yeah, was planning to, sorry about that, unfortunate that you cannot use a variable there

#7 ChunLing

  • Members
  • 2,027 posts

Posted 03 August 2012 - 08: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.

#8 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 03 August 2012 - 08: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

#9 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 06 August 2012 - 08:03 AM

View PostChunLing, on 03 August 2012 - 08:42 PM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users