Associative Array
[1/7] from: al:bri:xtra at: 14-Sep-2000 21:37
Has any one made an associative arrary in Rebol? Something like:
a/Associate Key Value
TheValue: a/find Key
It would be nice to show this to people used to using associative
arrays, that Rebol can do so as well.
Andrew Martin
ICQ: 26227169
http://members.ncbi.com/AndrewMartin/
http://members.xoom.com/AndrewMartin/
[2/7] from: dockimbel::free::fr at: 14-Sep-2000 14:24
>Has any one made an associative arrary in Rebol? Something like:
>a/Associate Key Value
>TheValue: a/find Key
>It would be nice to show this to people used to using associative
>arrays, that Rebol can do so as well.
>
>Andrew Martin
a: make block! 1 ; could be make hash! for faster key search
a/append [Key Value]
TheValue: a/select Key
That should make it ?
DK.
[3/7] from: kgd03011:nifty:ne:jp at: 14-Sep-2000 23:30
>Has any one made an associative arrary in Rebol? Something like:
> a/Associate Key Value
> TheValue: a/find Key
> It would be nice to show this to people used to using associative
>arrays, that Rebol can do so as well.
Hi Andrew,
Here's my latest attempt.
make-hash: func [][
make object! [
pos: none
keys: make hash! 1000
values: make block! 1000
set: func [key value][
either pos: find keys key
[poke values index? pos value]
[append keys key append values value]
]
get: func [key][
either pos: find keys key [pick values index? pos][none]
]
]
]
>> a: make-hash
>> a/set "brown" "chairo"
== ["chairo"]
>> a/set "green" "midori"
== ["chairo" "midori"]
>> a/set "brown" "kogechairo"
== ["kogechairo" "midori"]
>> a/get "brown"
== "kogechairo"
>> a/get "midori"
== none
Still, all the experimenting I've done indicates Rebol can't do
large associative arrays (more than a couple thousand keys)
nearly as efficiently as Perl.
Eric
[4/7] from: joel:neely:fedex at: 14-Sep-2000 15:47
With all due respect, let me suggest that we check out code snippets
bofore posting them to the list. The suggested code is extremely
broken:
Copyright 2000 REBOL Technologies. All rights reserved.
>> a: make block! 1 ; could be make hash! for faster key search
== []
>> a/append [Key Value]
** Script Error: Invalid path value: append.
** Where: a/append [Key Value]
>> TheValue: a/select Key
** Script Error: Invalid path value: select.
** Where: TheValue: a/select Key
A working variation is:
>> a: make block! 1
== []
>> append a [Key Value]
== [Key Value]
>> select a 'Key
== Value
>>
Note that the use of bare words in the append means that the target
passed to select must be a lit-word! or an expression that EVALUATES
to the key word:
>> foo: 'Key
== Key
>> select a foo
== Value
>> select a first [Key]
== Value
>> select a reduce ['Key]
== Value
That requirement may be a bit unobvious/tedious.
My big problem with using append and select with a block as an
implementation of associative arrays is that the keys and values are
not distinguished. Consider the imaginary product descriptions below:
>> appliance1: ["type" "tv" "screen" "b&w" "size" 19 "color" "woodgrain"]
== ["type" "tv" "screen" "b&w" "size" 19 "color" "woodgrain"]
>> appliance2: ["type" "tv" "screen" "color" "size" 21 "color" "charcoal"]
== ["type" "tv" "screen" "color" "size" 21 "color" "charcoal"]
Now let's look up some properties of these appliances:
>> select appliance1 "color"
== "woodgrain"
>> select appliance2 "color"
== "size"
>>
Since keys and values are all just in a big pile, there's no guarantee
that a select or find using "color" as a key won't accidentally hit
an occurrence of "color" as a value instead (giving me the next key as
the result of select ).
I'll follow up with an email containing a proposed implementation for
folks on the list to critique.
-jn-
[dockimbel--free--fr] wrote:
> >Has any one made an associative arrary in Rebol? Something like:
> >a/Associate Key Value
<<quoted lines omitted: 8>>
> That should make it ?
> DK.
--
; Joel Neely [joel--neely--fedex--com] 901-263-4460 38017/HKA/9677
REBOL [] print to-string debase/64 decompress #{
789C0BCE0BAB4A7176CA48CAB53448740FABF474F3720BCC
B6F4F574CFC888342AC949CE74B50500E1710C0C24000000}
[5/7] from: terrence_brannon:instinet at: 14-Sep-2000 14:29
This wont work when a value matches the key you are looking for and occurs prior
to the key you are looking for.
[dockimbel--free--fr] on 09/14/2000 08:24:51 AM
Please respond to [list--rebol--com]
To: [list--rebol--com]
cc: (bcc: Terrence Brannon/NYC/US/INSTINET)
Fax to:
Subject: [REBOL] Re: Associative Array
>Has any one made an associative arrary in Rebol? Something like:
>a/Associate Key Value
>TheValue: a/find Key
>It would be nice to show this to people used to using associative
>arrays, that Rebol can do so as well.
>
>Andrew Martin
a: make block! 1 ; could be make hash! for faster key search
a/append [Key Value]
TheValue: a/select Key
That should make it ?
DK.
[6/7] from: jhagman:infa:abo:fi at: 14-Sep-2000 22:03
Quoting [KGD03011--nifty--ne--jp] ([KGD03011--nifty--ne--jp]):
> Rebol can't do large associative arrays (more than a couple thousand
> keys) nearly as efficiently as Perl.
That should not be a surprise as associative arrays are implemented as
native in perl and interpreted in REBOL.
Yours,
Jussi
--
Jussi Hagman CS in Åbo Akademi University
Studentbyn 4 D 33 [juhagman--abo--fi]
20540 Åbo [jhagman--infa--abo--fi]
Finland
[7/7] from: dockimbel:free at: 15-Sep-2000 12:35
[joel--neely--fedex--com] a écrit :
> With all due respect, let me suggest that we check out code snippets
> bofore posting them to the list. The suggested code is extremely
<<quoted lines omitted: 16>>
> == Value
> >>
Man, i totally agree with you ! I apologize for this post, i was
surely a little bit drunk when i wrote that code !
DK, will keep the bottle away next time ;)
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted