Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] [Fwd: Re: Associative Array]

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 > >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.
-- ; Joel Neely [joel--neely--fedex--com] 901-263-4460 38017/HKA/9677 REBOL [] print to-string debase/64 decompress #{ 789C0BCE0BAB4A7176CA48CAB53448740FABF474F3720BCC B6F4F574CFC888342AC949CE74B50500E1710C0C24000000}