Attached is a script for collection object, I wrote it to get similar
functionality to the collection & dictionary objects in vb and vbscript.
(With a bit of modifiction it may suit your associative array needs).
Cheers,
Allen K
>>col make collection []
>> col/set-item "Apple" "Red"
>> col/set-item "App" "Office"
>> col/get-item "App"
== "Office"
>> col/get-item "Apple"
== "Red"
>> col/get-item 1
== "Red"
>> col/get-item 2
== "Office"
----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, March 10, 2000 10:05 PM
Subject: [REBOL] Associative Arrays in Rebol? Re:(2)
> In article <[EMAIL PROTECTED]>, you wrote:
> >I think select and paths should come close to what you're asking:
>
> >>> select [ a "this is a" b "this is b" ] 'a
> >== "this is a"
>
> I'll have to ponder that for a while but my first thought
> is that doing it that way would make it difficult to
> populate it from a data file of arbitrary data.
>
> Here is a snipit from the Awk doc's that explains what I'm
> after.
>
> --------------------------------------------------------
> http://www.gnu.org/manual/gawk-3.0.3/html_chapter/gawk_12.html
>
> Arrays in awk superficially resemble arrays in other
> programming languages; but there are fundamental
> differences. In awk, you don't need to specify the size of
> an array before you start to use it. Additionally, any
> number or string in awk may be used as an array index, not
> just consecutive integers.
>
> In most other languages, you have to declare an array and
> specify how many elements or components it contains. In such
> languages, the declaration causes a contiguous block of
> memory to be allocated for that many elements. An index in
> the array usually must be a positive integer; for example,
> the index zero specifies the first element in the array,
> which is actually stored at the beginning of the block of
> memory. Index one specifies the second element, which is
> stored in memory right after the first element, and so on.
> It is impossible to add more elements to the array, because
> it has room for only as many elements as you declared. (Some
> languages allow arbitrary starting and ending indices, e.g.,
> `15 .. 27', but the size of the array is still fixed when
> the array is declared.)
>
> A contiguous array of four elements might look like this,
> conceptually, if the element values are eight, "foo", "" and
> 30:
>
> Only the values are stored; the indices are implicit from
> the order of the values. Eight is the value at index zero,
> because eight appears in the position with zero elements
> before it.
>
> Arrays in awk are different: they are associative. This
> means that each array is a collection of pairs: an index,
> and its corresponding array element value:
>
> Element 4 Value 30
> Element 2 Value "foo"
> Element 1 Value 8
> Element 3 Value ""
>
> We have shown the pairs in jumbled order because their order
> is irrelevant.
>
> One advantage of associative arrays is that new pairs can be
> added at any time. For example, suppose we add to the above
> array a tenth element whose value is "number ten". The
> result is this...
>
>
REBOL [
Title: "Collection Object"
Author: "Allen Kamp"
Date: 11-Sep-1999
Email: [EMAIL PROTECTED]
Purpose: {To provide an object similar to the VB collection object.}
Notes: {I should hide the values and attributes lists (or make read only).
And add a method to return a paired block, for iteration,
also add a clone method}
Usage: {
>> col: make collection []
>> col/set-item "Apple" "Green"
>> col/set-item "Orange" "Orange"
>> col/set-item "Grapes" "Purple"
>> col/count
== 3
>> col/get-item "Apple"
== "Green"
>> col/set-item "Apple" "Red"
>> col/get-item "Apple"
== "Red"
>> col/exists "Apple" ;--Return the index or else false
== 1
>> col/get-item 1
== "Red"
>> col/remove-item "Apple"
>> col/get-item "Apple"
== none
;-- if you want to add to the beginning of the list
col/set-item/pos "Apple" "Granny Smith" 1
;-- to return the list of attributes or values
col/attributes
col/values
}
]
collection: make object! [
values: make block! [] 30
attributes: make hash! [] 30
;---count
count: func [][length? attributes]
;---exists
exists: func [
attribute [any-string!]
/local mark
][
either found? mark: find attributes attribute [return (index? mark)][return
false]
]
;---Remove-all
remove-all: func [][attributes: make hash! [] 30 values: make block! [] 30 exit]
;---remove-item
remove-item: func [
key [any-string! integer!]
/local index
][
either integer? key [
index: key
][
if not index: exists key [exit]
]
remove at attributes index
remove at values index
exit
]
;---get-item
get-item: func [
{Returns the value for the key or index. Returns none if does not exist}
key [any-string! integer!] {Attribute name string or an index}
/local index
][
if not integer? key [
either index: exists key [
key: index
][
key: 0
]
]
return pick values key
]
;---set-item
set-item: func [
key [any-string!] {Attribute name string}
value
/pos position [integer!] {Specify a position in the list}
/local index
][
either pos [
insert at attributes position key
insert/only at values position value
exit
][
either index: exists key [
change/only at values index value
exit
][
insert tail attributes key
insert/only tail values value
exit
]
]
]
]