Archive

Posts Tagged ‘load’

Store Data Records in Files

How to save multiple data records to a file and load them back again.

Let’s say you have a block of name records sort of like this:

group: [
    [1 "Bob" "Smith" 12-Aug-1965 "Seattle"  WA member]
    [2 "Tom" "Able"  20-Feb-1955 "San Jose" CA author]
    [3 "Jen" "Jones" 16-Jun-1972 "New York" NY leader]
    [4 "Sal" "Baker" 25-Oct-1975 "Boston"   MA member]
]

To save all of these to a file:

save/all %group.r group

Take a look at the group.r file in an editor, and you’ll be able to see the same records as above.

To load back the records you saved above:

group: load/all %group.r

Don’t forget to use the /all refinement or you’ll have some problems at times.

Now you have an easy way to save and access data from your programs. This technique even works well for files of more than 100 thousand records!

Categories: Storing Data Tags: , , , ,

Store Simple Values in Files

This example shows how to save data out to a file and loading it back again.

For simple single values, you just save them out to a file.

To store an integer:

num: 123
save %num.r num

To store a string:

name: "Tom Smith"
save %name.r name

You can use any suffix for the file. We use .r here because the data is in REBOL format, but the suffix doesn’t matter.

Now, you use load to get back the values.

To load the integer:

num: load %num.r
print num
123

To load the string:

name: load %name.r
print name
Tom Smith
Categories: Storing Data Tags: , , , ,