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

Searching objects

 [1/5] from: robert:muench:robertmuench at: 13-Dec-2001 9:22


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

 [2/5] from: carl::cybercraft::co::nz at: 14-Dec-2001 0:46


On 13-Dec-01, 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
The following function might do the job for you, though I's sure there must be a better way... obj-find: func [series field value][ forall series [if series/1/:field = value [return index? series]] none ] s: reduce [ make object! [a: 10 b: "a"] make object! [a: 20 b: "b"] make object! [a: 30 b: "c"] make object! [a: 40 b: "d"] ]
>> obj-find s 'a 20
== 2
>> obj-find s 'a 50
== none
>> obj-find s 'b "d"
== 4 -- Carl Read

 [3/5] 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
<<quoted lines omitted: 3>>
> 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" ;

 [4/5] from: robert:muench:robertmuench at: 13-Dec-2001 18:09


> -----Original Message----- > From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]On Behalf Of
<<quoted lines omitted: 5>>
> objects explicitly to be searchable on whatever criteria you > have in mind? Quick-and-dirty sample follows:
Hi, nice idea to make it generic on each data-field :-)).
> phones-by-name: func [name [string!]] [ > foreach chum blackbook [
<<quoted lines omitted: 4>>
> return copy [] > ]
This solution uses a loop too. If possible I would like to avoid looing through the objects. I had something like using indices in mind. With this I would have something like this: myobjects: make hash! [] ; using index,object pairs = [1 obj1 2 obj2] myobjects_names: make hash! [] ; using data-field, index pairs = ["robert" 1 "carl" 2 "joachim" 3] Searching the object with a name joachim would result in object ID 3, than makeing a lookup in the objects and get the object with ID 3. I hope you know what I mean. With this you will have an index for each data-field you want to search for. Remembers me on the good old databases ;-)) Robert

 [5/5] from: robert:muench:robertmuench at: 13-Dec-2001 18:08


> -----Original Message----- > From: [rebol-bounce--rebol--com] [mailto:[rebol-bounce--rebol--com]]On Behalf Of
<<quoted lines omitted: 8>>
> none > ]
Hi, thanks but that's the O(n) version IIRC? I would like to use the hash! approach/speed but wasn't able to find the right way to do it ;-)). Robert

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted