[REBOL] Re: Searching objects
From: joel:neely:fedex at: 13-Dec-2001 7:16
Hi, Robert,
Robert M. Muench
wrote:
> Hi, I have the following problem:
>
> I'm handling objects, with several data-fields, in a series
> and now want to find an object with a specific value in a
> data-field. The problem is that 'find doesn't browse through
> the values.
>
> Any tip how I can search for values in data-fields of objects?
>
> Robert
>
I'm sure someone can polish this, but how about setting up the
objects explicitly to be searchable on whatever criteria you
have in mind? Quick-and-dirty sample follows:
8<------------------------------------------------------------
searchable-object: make object! [
searchable-fields: []
get-searchable-fields: func [][compose searchable-fields]
]
friend: make searchable-object [
name: ""
phones: []
searchable-fields: [(name) (phones)]
]
blackbook: []
phones-by-name: func [name [string!]] [
foreach chum blackbook [
if find chum/get-searchable-fields name [
return chum/phones
]
]
return copy []
]
name-by-phone: func [phone [issue!]] [
foreach chum blackbook [
if find chum/get-searchable-fields phone [
return chum/name
]
]
return copy []
]
8<------------------------------------------------------------
After the following setup...
append blackbook make friend [
name: "John Q. Public"
phones: [#800-555-1212 #123-4567]
]
append blackbook make friend [
name: "Jane R. Private"
phones: [#123-456-7890 #111-222-3333 #321-654-0987]
]
append blackbook make friend [
name: "Kermit T. Phrogg"
phones: [#011-127-394-56378]
]
this behaves as follows:
>> phones-by-name "John Q. Public"
== [#800-555-1212 #123-4567]
>> name-by-phone #123-456-7890
== "Jane R. Private"
>> blackbook/3/get-searchable-fields
== ["Kermit T. Phrogg" #011-127-394-56378]
HTH!
-jn-
--
; sub REBOL {}; sub head ($) {@_[0]}
REBOL []
# despam: func [e] [replace replace/all e ":" "." "#" "@"]
; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"}
print head reverse despam "moc:xedef#yleen:leoj" ;