[REBOL] Re: The all function.
From: joel:neely:fedex at: 15-Apr-2002 6:50
Hi, Louis,
Dr. Louis A. Turk
wrote:
> From studying what both of you have said, I think that the root
> problem may be that I have a basic design flaw in my database.
> This is (I think) what has confused me, and also is perhaps what
> has made it difficult for you to understand my problem. I think
> I only have one key but need two keys. (Or maybe I don't need
> any keys at all! Is only one key per record
> allowed?)
>
...
> ... Note, however, that although the records are all unique,
> the code data is not necessarily unique, nor is the chknum data.
> There may be many records with the same code, and also many
> records with the same chknum. Only the code / chknum combination
> is unique.
>
Then my suggestion is to use a combination of those two attributes
as the key.
> So, do I perhaps need the records to look like this?:
>
> ] "bho" "5259" ; <===<<< TWO KEYS.
> make object! [
> code: "bho"
> chknum: "5259"
> date: 27-Jul-2001
> amount: $100.00
> special: $0.00
>
How about
...
"bho:5259" ; <===<<< COMBINED key
make object! [
code: "bho"
chknum: "5259"
...
]
...
> all [find database code find database chknum]
>
That wouldn't work (with the "two key" version you conjectured)
because it fails (yields NONE) if neither value is present
*anywhere* in DATABASE and succeeds (yields a subseries of
DATABASE) if both values occur *somewhere* in DATABASE, but the
two occurrences could be uncorrelated...
>> database: [
[ "A" "1" #dummy
[ "B" "2" #dummy
[ "C" "3" #dummy
[ ]
== [
"A" "1" #dummy
"B" "2" #dummy
"C" "3" #dummy
]
>> all [find database "A" find database "3"]
== ["3" #dummy
]
...whereas you apparently want to know only if the *combination*
of the two values is present. If the two values may occur (with
other neighbors) multiple times, then you'd need a more complex
expression (i.e. one involving an explicit loop AFAICT) to keep
re-trying until all occurrences of one value have been checked
to see whether one of them is paired with the other value.
This way lies madness. (e.g. what happens when you need to
check a combination of 3 key fields...?)
> Am I correct about all this? If so, won't the following functions
> have to be rewritten?:
>
> load-data: has [data] [
> if exists? db-file [
> data: reduce load/all db-file
> clear database
> if data [
> foreach item data [
> item: make record item
> repend database [item/code item] ;item/key_field item
> ]
> ]
> database: make hash! database
> ]
>
> ]
>
Just modify the above to contain the following instead:
foreach item data [
item: make record item
repend database [
rejoin [item/code ":" item/chknum] item
] ;item/key_field item
]
and make the corresponding changes to the other functions.
HTH!
-jn-
--
; Joel Neely joeldotneelyatfedexdotcom
REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip
do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] {
| e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]