[thelist] CF: using a back reference to address a struct?

Mark Mandel mark.mandel at gmail.com
Wed Jun 3 15:30:28 CDT 2009


>From looking at it, you're trying to do a loop with only one line of code,
which, without closures is going to be pretty tricky.

I would just do it as (this is typing on the fly, so you may get some
errors):

<cfloop collection="#filter#" item="key">
   <cfset text = replaceNoCase(text, "[" & key & "]", filter[key]) />
</cfloop>

And be done with it.

You could get fancy and use a StringBuffer/Builder if you wanted to, but for
the number of lines of text, it may not be necessary.

Mark

On Wed, Jun 3, 2009 at 6:50 PM, Frank Marion <lists at frankmarion.com> wrote:

> Hey all! Long time no chat!
>
> I'm trying to create a function, much like a user-defined BBCode where
> a user can have all instances of [any_given_value] replaced with its
> value stored in a struct (and if no matching value exists, that the
> [value] is ignored). I'm trying to use a back-reference from
> REReplace, but no matter how many weird variations of addressing,
> using DE or even Evaluate I use, I can't seem to get it knocked off.
>
> If I use "filter[\1]", my result is: filter[company_name]
>
> If I use "#filter[\1]#", my result is: Missing argument name
>
> If I use "#filter['\1']#", my result is: Element \1 is undefined in a
> structure references as part of an expression.
>
> Can anyone help me figure this out? Alternately, if you have a simpler
> or more effective way of doing it, I'm all ears.
>
> Look for the REReplace at the variable named text_filtered.
>
> Thanks!
>
>
> <!--- First, the struct --->
> <cfscript>
>    filter = StructNew();
>    filter["company_name"] = "ABC Inc";
>    filter["company_usp"] = "We do stuff";
>    filter["company_address"] = "123 Main Street";
>    filter["company_phone"] = "919 555 1212";
>    filter["company_president"] = "Mr. Wilbur Burrows";
> </cfscript>
>
> <cfsavecontent variable="text">
> This is the story of a company called <strong>[company_name]</strong>.
> They say that their motto is <strong>[company_usp]</strong>. One day,
> the president, a certain <strong>[company_president]</strong> used a
> <strong>[fake_name]</strong> and called the company at
> <strong>[company_phone]</strong> to find out if the address was really
> <strong>[company_address]</strong>. It was.
> </cfsavecontent>
>
>
> <cfset text_filtered = REReplaceNoCase(text,"\[([a-zA-Z0-9-_]+)
> \]","filter[\1]","all")>
>
>
> <div style="border: 1px solid black;padding:10px;">
>    <p><strong>Before</strong><br>
>    <cfoutput>#text#</cfoutput></p>
> </div>
>
> <div style="border: 1px solid red;padding:10px;">
>    <p><strong>After</strong><br>
>    <cfoutput>#text_filtered#</cfoutput></p>
> </div>
>
>
> --
> Frank Marion
> lists [_at_] frankmarion.com
>
>
>
>
>
> --
>
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
>



-- 
E: mark.mandel at gmail.com
W: www.compoundtheory.com



More information about the thelist mailing list