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

[REBOL] Re: Block building

From: gscottjones:mchsi at: 24-Nov-2002 14:51

Hi, Ted, From: Ted Landis
> I am relatively new to REBOL and would like to know what is considered > best practice in the following situation: > > Given the following object: > > >> probe Object1 > > make object! [ > String1: "ABC" > Block1: ["DEF" "GHI"] > Block2: ["JKL" "MNO" "PQRSTUVWXYZ"] > ] > > I need to build a block containing all of the string fields: > ["ABC" "DEF" "GHI" "JKL" "MNO" "PQRSTUVWXYZ"] > > I have found two ways to do it: > > >> compose [(Object1/String1) (Object1/Block1) (Object1/Block2)] > > and > > >> head insert (union Object1/Block1 Object1/Block2) Object1/String1 > > Comments? Suggestions?
I think that the expression, "There is more than one way to do it" is apropos. The best way is the way that works for you and makes sense when you look at it later. :-) Another variation on what you have already discovered includes: object1: make object! [ String1: "ABC" Block1: ["DEF" "GHI"] Block2: ["JKL" "MNO" "PQRSTUVWXYZ"] ] blk: copy [] append blk object1/string1 append blk object1/block1 append blk object1/block2 If you have really dynamic code, and therefore don't know in advance what components are in the object, you can take advantage of getting 'first on the object:
>> first object1
== [self String1 Block1 Block2]
>From the second position and on, you will have the names of the various
components. So, the following iterates through each of these, getting the value and appending to a blk (watch for line breaks): object1: make object! [ String1: "ABC" Block1: ["DEF" "GHI"] Block2: ["JKL" "MNO" "PQRSTUVWXYZ"] ] blk: copy [] foreach component next first object1 [ append blk get in object1 component] I'm sure that there will be other responses that show other ways. HTH --Scott Jones