Archive

Posts Tagged ‘example’

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!

Advertisements
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: , , , ,

How to run a script

This example shows how to run a script from the console or from another script.

‘To run any script, you can type:

do %script.r

The do word means load the file script.r and evaluate it. (Note that in REBOL, they evaluate code, not execute it.)

The % indicates that the string following it is a file name or file path.  It’s a unique datatype called file! That’s an important. REBOL has a several different string datatypes and you will use them for different purposes. More on that later, but for now, just don’t forget the % in front of the filename.

You can put do in scripts too. That’s how you include other files in your program.

For example:

do %dictionary.r
do %html-tags.r
do %html-forms.r
do %name-database.r

This would evaluate and include four other files in your program.

The filename can be a variable also. For example:

tagfile: %html-tags.r
do tagfile

So, you could even do something clever like:

foreach file [%dictionary.r %html-tags.r %html-forms.r] [
    do file
]
Categories: Evaluation Tags: , ,