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

[REBOL] Re: Parse refactoring puzzle

From: joel:neely:fedex at: 31-Jul-2003 16:05

Hi, Sunanda, This sounds familiar, and I suggest that PARSE is overkill... [SunandaDH--aol--com] wrote:
> Yes, I have very long strings -- the place-holders are embedded in template > HTML pages, so several to many K is typical -- so fast solutions are what I > need. >
I've solved a similar problem in the past like so: 1) Put the tags and corresponding values in a block, as in: data: [ "!!name!!" "John Doe" "!!age!!" "23" "!!present!!" "new car" ] 2) Use a string for the message template, with embedded tags: message: { Hi, !!name!!, Happy birthday! On this day when you turn !!age!!, we'd like to celebrate with you. You should be receiving our present, a !!present!! in the mail. Every !!age!!-year-old should have a !!present!!, don't you think? Again, !!name!!, happy !!age!!! Your friends } 3) Use the following simple function to return a filled-in template based on the tag/value pairs: fill-it: func [msg [string!] stuff [block!] /local result] [ result: copy msg foreach [tag value] stuff [ replace/all result tag value ] result ] 4) Get results as follows:
>> print fill-it message data
Hi, John Doe, Happy birthday! On this day when you turn 23, we'd like to celebrate with you. You should be receiving our present, a new car in the mail. Every 23-year-old should have a new car, don't you think? Again, John Doe, happy 23! Your friends
>>
If you start with a default data block default_data: [ "!!name!!" "" "!!age!!" "" "!!present!!" "" ] and clone and fill in the values for each use, then any value you don't replace is the default (which can be an empty string as above to make unused tags disappear). HTH -jn- -- ---------------------------------------------------------------------- Joel Neely joelDOTneelyATfedexDOTcom 901-263-4446 Counting lines of code is to software development as counting bricks is to urban development.