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

[REBOL] Re: How to remove words from objects?

From: ingo:2b1 at: 13-Jan-2004 12:17

Hi Luke, Luke wrote:
> Dear list > > I'm trying to dynamically add and remove words from an > object. So far I can do the add words as follows: > >>>obj: make object! [a: 1 b: 2] >>>probe obj > > make object! [ > a: 1 > b: 2 > ] > >>>obj: make obj [c: 3] >>>probe obj > > make object! [ > a: 1 > b: 2 > c: 3 > ] > > now I want to remove c from obj. >> obj: make object! remove/part find third obj to set-word! 'c 2 >> probe obj
make object! [ a: 1 b: 2 ] The main part in this expression is "third obj", which returns the object as a block, like this:
>> third obj
== [a: 1 b: 2] After that it boils down to removing the part that constructs the element you want to remove, and use the new block to construct your object. BUT BEWARE: you are always constructing NEW OBJECTS! This means, that references still point to the old object, e.g.
>> obj: make object! [a: 1 b: 2] >> a: []
== []
>> insert a obj
== []
>> probe a/1
make object! [ a: 1 b: 2 ]
>> obj: make obj [c: 3] >> probe obj
make object! [ a: 1 b: 2 c: 3 ]
>> probe a/1
make object! [ a: 1 b: 2 ] ; !!! a contains a reference to the old 'obj So, it works well if you have only well defined references to your objects, otherwise just use block!s for very dynamic entities. The downside is that lookup in a block is not as fast as in an object. I hope that helps, Ingo