[REBOL] Little data trick
From: jeff::rebol::com at: 18-May-2001 11:09
Here's a little trick.
Say you've got a little database you're making and sometimes
you want to use only one field and sometimes you want to use
two fields. For example you want to have a block of email
and urls, but sometimes you only want to do searches over
the emails and sometimes you want to get the urls from the
emails as a key.
Someone might do this:
email-url-db: [
[a--example--com] http://www.example.com/a
[b--example--net] http://www.example.net/b
[c--example--org] http://www.example.org/c
[d--example--edu] http://www.example.edu/d
]
just-emails: copy []
foreach [em url] email-url-db [append just-emails em]
just-emails: make hash! just-emails
email-url-db: make hash! email-url-db
That's okay, but you can be sneaky if you save your database
on disk so that it looks like this:
;===== %example-db.dat =================
[a--example--com] ; http://www.example.com/a
[b--example--net] ; http://www.example.net/b
[c--example--org] ; http://www.example.org/c
[d--example--edu] ; http://www.example.edu/d
;=========================================
Now you can just do:
dbf: %example-db.dat
just-emails: load dbf
email-url-db: load replace/all read dbf ";" #
So when your script gets a new email and url you just add to the
database:
write/append dbf reform [new-email ";" new-url newline]
You can of course extend the principal to as many fields as
you like, put you just have to strip as many needed
semicolons from each line before loading (read/lines).
Important Caveat: You have to be sure that semicolon won't
show up legitimately in any of your fields (safe with emails
and urls). So while saving you would check to make sure the
data doesn't contain the separator and change it
appropriately. Anyhow, I just thought the trick was cute
and figured I'd share. YMMV.
-jeff