Wow... so many questions. It's a good think I can't sleep tonight...
My replies are mixed in below:
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of
> Dr. Louis A. Turk
> Sent: Tuesday, April 10, 2001 11:48 PM
> To: [EMAIL PROTECTED]
> Subject: [REBOL] Re: Object Database
>
>
> Dear Carl,
>
> I am amazed that such a small amount of code can do so much, and
> also that
> the creator of this language himself would help me like this.
> Many thanks!
Whatever I can do to help!
> I have some questions. I have been studying your code carefully
> trying to
> learn how it works. I've been on this list for quite a while, but have
> only recently taken time to buy Ralph's book and Elan and John's book to
> really get serious about learning the language. I'm a real beginner, so
> please excuse me if my questions betray that fact.
>
> So far I have figured out how to use your functions to enter data into a
> record, and then save it to a file. By entering "print read %data.r" at
> the command line, I am able to see that I have entered the data
> successfully. So I think I understand how to use insert-data and
> save-data. However, when I type load-data I get an error message ("blk
> has no value"); what am I doing wrong? Also, I assume that I must learn
I'm sorry. Change blk to data. A typo. Did I mention that it still
needed your help debugging it? ;)
> how to get load-data to work before find-data and remove-data will work;
> but to keep from having to ask you later, how are these two
> functions used?
The database works from memory. Load-data brings it into memory where the
find, remove and other functions operate on it. So, yes, you have to load
it first, or at least insert-data a few times to create some data records.
The functions work like this:
rec: find-data [EMAIL PROTECTED]
print rec/name
print rec/phone
etc.
remove-data [EMAIL PROTECTED]
save-data ; write back to disk
insert-data [EMAIL PROTECTED] "Kitty Carson" none http:/www.a.com
save-data ; write back to disk
You can use NONE for any missing value above. You don't need to do the
save-data each time... but you must do it sooner or later.
> I want to use this database for an email list. Most of the data remains
> the same, but email addresses change often. Sometime I do not
> even have a
> name---only an email address that has to be removed or changed. How do I
> change an email address?
The data is organized (keyed) by email. To change an email address, but
keep the rest of the record intact, you'll need a new function:
change-data: func [email-old email-new /local record] [
record: find database email-old
if none? record [alert reform ["Missing record:" email-old] exit]
insert clear first record email-new ; see note below
]
Note: This is subtle, so should be noted... I'm clearing out the
actual memory string for the old email, then inserting the new string
into it.
Also, this string is shared. It is used both in the database
block (as the key) and in the record object itself. So changing it
here will change it in both places. That's a feature, but it is not
obvious, so make sure that you comment that line.
> You say:
>
> > If you expect to grow this database to a large
> >size, you will want to MAKE HASH! the database when you load
> >it.
>
> Will this modification do it?:
>
> >load-data: has [data] [
> > data: make hash! load/all db-file
> > clear database
> > foreach item blk [
> > item: make record item
> > repend database [item/email item]
> > ]
> >]
You will want to make the hash after the database is created
(after the foreach loop).
database: make hash! database
But, don't worry about that until your database gets big. For
a few hundred names, you don't need it. Get to know the code
and debug it before hashing it.
> Will your code work with Rebol/View also? Am I correct in assuming that
> View would be ideal for creating a user interface for the database?
Yes and yes. You will want to add two new functions: one to copy
data from a record into a view form, and another to copy it back
or create a new record in the database.
> Many thanks for your help, and for creating a truly exciting language.
When you are done, you will have written an actual database system
from scratch in REBOL. Not bad for a couple pages of code, eh?
> Louis
>
>
>
> At 08:28 AM 4/9/2001 -0700, you wrote:
> >What!? You just want us to just whip out an object database, eh? ;)
>
> Well, I wasn't expect anyone to whip out one especially for me,
>
> >Ok, why not.... Here is a small "contacts" database keyed by email.
> >You can expand or reduce the record definition without corrupting
> >or affecting the database.
> >
> >REBOL [Title: "Email Contact Database"]
> >
> >db-file: %data.r
> >record: context [name: email: phone: web: none]
> >database: []
> >
> >load-data: has [data] [
> > data: load/all db-file
> > clear database
> > foreach item blk [
> > item: make record item
> > repend database [item/email item]
> > ]
> >]
> >
> >save-data: has [data] [
> > data: copy []
> > foreach [key obj] database [
> > append/only data third obj
> > ]
> > save db-file data
> >]
> >
> >find-data: func [email] [select database email]
> >
> >remove-data: func [email] [remove/part find database email 2]
> >
> >insert-data: func [email' name' phone' web'] [
> > repend database [
> > email'
> > make record [
> > email: email'
> > name: name'
> > phone: phone'
> > web: web'
> > ]
> > ]
> >]
> >
> >You can expand/contract the record definition at any time.
> >
> >This is untested... but should be close to working, less a few
> >minor typos. If you expect to grow this database to a large
> >size, you will want to MAKE HASH! the database when you load
> >it.
> >
> >-Carl
> >
> >PS: The remove/part on find really does work. Remove none is
> >allowed in Core 2.5.
> >
> >
> >-----Original Message-----
> >From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of
> >Dr. Louis A. Turk
> >Sent: Monday, April 09, 2001 4:35 AM
> >To: [EMAIL PROTECTED]
> >Subject: [REBOL] Object Database
> >
> >
> >I am trying to learn how to make an object database using REBOL.
> I need a
> >simple, but complete, working example. Can anyone supply one?
> >
> >Also, what are probably dumb questions:
> >
> >1. If an object database contains data, will that data be lost if another
> >field is added?
> >
> >2. Is there any way to change the name of an object in a database without
> >losing the data in the object?
> >
> >3. Would it be possible to use email addresses for the names of
> the objects?
> >
> >Thanks in advance for any help.
> >
> >Louis
> >
> >
> >--
> >To unsubscribe from this list, please send an email to
> >[EMAIL PROTECTED] with "unsubscribe" in the
> >subject, without the quotes.
> >
> >
> >--
> >To unsubscribe from this list, please send an email to
> >[EMAIL PROTECTED] with "unsubscribe" in the
> >subject, without the quotes.
>
> --
> To unsubscribe from this list, please send an email to
> [EMAIL PROTECTED] with "unsubscribe" in the
> subject, without the quotes.
>
>
--
To unsubscribe from this list, please send an email to
[EMAIL PROTECTED] with "unsubscribe" in the
subject, without the quotes.