One example that I have is when I have a free text field, (ie: in a form
builder) and the person building the form is allowed to use coldfusion
variables to pull out values at run time.  I parse he string for any #
symbols and then build an appropiate string to evaluate.  I find evaluate
works very well in this situation as I have now idea what will be in the
text field.

I do agree, however that it should not be used unless alternatives have been
considered.  I also think that there is not a noticeable performance hit
when using it in cf7.

For those that are interested my 2 functions which will parse any text are
below.  The one to call is evaluateString("your text with possible cf
variables in it").

Just in case someone asks ,  I looked at tokensing the string but found the
method I use to be fast and also made it easier to deal with complex
variables.



//hint=Evaluate a string looking for any variable names then process and
return completed string
//     Example 1 "The value for email is #headEmail#"
//     Example 2 "The http referer is #http_referer# or #cgi.http_referer#
//  NB:  I have decided to use the find function with mid
//       The other options would have been to tokenise the string or to
strip the original
//       string down as I am processing to make it shorter an remove the mid
function
function evaluateString(str)
{
  var newStr = "";
    var finished = false;
    var itterations = 0;
    var marker = 1;
    var spos = 1;
    var epos=1;
    var strLen = len(str);
    var evaled_var = "";
    var midStr = "";
  var startBPos = 0; // End bracket position
  var endBPos = 0; // End bracket position
    var startVar = "";
    var endVar = "";
    var variable = "";
    var tempTxt = "";

    //writeOutput("<br>evaluateString(#htmlEditFormat(str)#) of length
#len(str)#<br>");
    if (find("##", str, 0) gt 0)
    {
        do
        {
            spos = 0;
            if (marker lte strLen)
                spos = find("##", str, marker);
            if (spos eq 0 or spos eq strLen)
            {
                // Then we have hit the end of any variables.  Finish string
                //writeOutput("Hit end of string<br>");
                finished = true;
                if (marker lte strLen)
                    newStr = newStr & mid(str, marker, strLen - marker + 1);
            }
            else
            {
                midStr = mid(str, marker, spos - marker);
                //writeOutput("midStr = [#htmlEditFormat(midStr)#]<br>");
                newStr = newStr & midStr;
                marker = spos; // Move marker along
                // Look for 2 pounds together
                if (mid(str, marker +1 , 1) eq "##")
                {
                  // Then we found a double pound
                    //writeOutput("Found [####]<br>");
                    newStr = newStr & "##";
                    marker = marker + 2;
                }
                else
                {
                  // Look for the closing pound
                    epos = find("##", str, marker + 1);
                    if (epos gt 0)
                    {
                        variable = mid(str, spos + 1, epos - spos -1);
                        //writeOutput("variable =
[#htmlEditFormat(variable)#]<br>");
                        if (find("(",variable) gt 0)
                        {
                            // Only need to make recursive call if there is
an array
                            // cause of the limitations of cfmx
                            if (find("[", variable, 1))
                            {
                                // Pull out what is between the ()
                                startBPos = find("(", variable, 1);
                                endBPos = find(")", variable, 1);
                                tempTxt = mid(variable, startBPos + 1,
endBPos - startBPos - 1); // No braces please
                                // Processes the middle text
                                tempTxt = evaluateString("###tempTxt###");
                                tempTxt = replace(tempTxt, """", """""",
"All");
                                startVar = mid(variable, 1, startBPos);
                                endVar = mid(variable, endBPos, strLen);
                                //writeOutput("Want to evaluate
#htmlEditFormat("[#startVar#][#tempTxt#][#endVar#]")#<br>");
                                tempTxt = "#startVar#""#tempTxt#""#endVar#";
                                evaled_var = evaluate("#tempTxt#");
                            }
                            else
                              evaled_var = evaluate(variable);
                        }
                        else if (find("[",variable) gt 0)
                            evaled_var = findStructArrayValue("#variable#");
                        else if (isDefined("caller.#variable#"))
                          evaled_var = evaluate("caller.#variable#");
                        else if (isDefined(variable))
                          evaled_var = evaluate(variable);
                        else if (isDefined("session.eForms.values.#variable#
"))
                          evaled_var = evaluate("
session.eForms.values.#variable#");
                        else
                          evaled_var = "{null}";
                        newStr = newStr & evaled_var;
                        marker = epos + 1;
                        evaled_var = "";

                        // If at the end if the string then finish
                        if (epos eq strLen)
                            finished = true;
                    }
                }
            }
            itterations = itterations + 1;
            //writeOutput("itteration [#itterations#] marker=[#marker#]
spos=[#spos#] epos=[#epos#] finished=[#finished#] :
[#htmlEditFormat(newStr)#]<br>");
        }
        while (finished eq false and itterations lte 1000);
    }
    else
        newStr = str;

    //writeOutput("return [#htmlEditFormat(newStr)#]<br>");
    return newStr;
}


//hint=If given a string that is a pointer to a structure with an array in
it then find the associated value
// Example 1 session.eForms.keys.pages[7].trigger.prePage.email.to
function findStructArrayValue(str)
{
    var sStr = ""; // Start string
    var eStr = ""; // End string
  var startBPos = 0; // End bracket position
  var endBPos = 0; // End bracket position
    var newStr = "";
    var element = 0;
    var x = 1;
    var lLen = listLen(str, "."); // Length of the struct element list
    var finished = false;
    var itterations = 0;
    var curStruct = "";
    var arrayPos = 1; // The array position for  scalar variable when found
    var arrayTxt = ""; // The stuff in the brackets

    //writeOutput("<br>findStructArrayValue(#htmlEditFormat(str)#) of length
#len(str)#<br>");
    if (isDefined(listGetAt(str, x, ".")))
    {
        sStr = listGetAt(str, x, ".");
        curStruct = evaluate(sStr); // Get first item as struct. Just a
pointer according to Macromedia
        do
        {
            x = x + 1;
            itterations = itterations + 1;
            if (x gt lLen)
            {
                newStr = "<strong>Ran out of elements processing
""#str#""</strong>";
                finished = true;
            }
            else
            {
                element = listGetAt(str, x, ".");

                // Process array if required
                startBPos = find("[", element, 1);
                if (startBPos gt 0)
                {
                    arrayTxt = mid(element, startBPos + 1, len(element) -
startBPos - 1); // No braces please
                    element = mid(element, 1, startBPos - 1);
                    //writeOutput("Finding array [#element#] with var
[#arrayTxt#]<br>");
                    if (isNumeric(arrayTxt))
                        arrayPos = arrayTxt;
                    else
                        arrayPos = evaluateString(arrrayStr);
                    //writeOutput("Array Pos [#arrayPos#]<br>");
                }

                //writeOutput("Processing element [#element#]<br>");

                if (structKeyExists(curStruct, element))
                {
                    if (x eq lLen)
                    {
                        // A the end and we have a value
                      newStr = structFind(curStruct, element);
                        if (isArray(newStr))
                            newStr = newStr[arrayPos]; // Worked out
previously and if not then it defaults to 1
                        finished = true;
                    }
                    else
                    {
                        // Move to next struct element
                        curStruct = structFind(curStruct, element);
                        if (isArray(curStruct))
                            curStruct = curStruct[arrayPos]; // Worked out
previously and if not then it defaults to 1
                    }
                }
                else
                {
                    newStr = "<strong>Element ""#listGetAt(str, x, ".")#""
missing from ""#str#""</strong>";
                    finished = true;
                }
            }
        }
        while (finished eq false and itterations lte 1000);

    }
    else
        newStr = "<strong>Element ""#listGetAt(str, 1, ".")#"" missing from
""#str#""</strong>";

    //writeOutput("return [#htmlEditFormat(newStr)#]<br>");
    return newStr;
}





On 08/12/06, Brett Payne-Rhodes <[EMAIL PROTECTED]> wrote:
>
>
> Good points all...
>
> What is needed are some good examples so that we can understand what the
> alternatives are and why they are better than evaluate(). Examples of what
> evaluate *should* be used for would also be useful.
>
> For me, dynamic forms were where I learnt to NOT use evaluate.
>
> a form field like <input name="name#userId#">
> I would access via something like <cfset theName = evaluate("
> form.name#userId#")>
>
> When in reality I can use <cfset theName = form["name#userId#"]>
>
> which might not be much different in execution time but is a much better
> coding practice simply because it reduces the complexity of the code for the
> trainee programmer who comes along to alter it later.
>
> Can anyone supply a simple example of an 'acceptable' use of evaluate()?
>
> Cheers,
>
> Brett
> B)
>
>
> Adam Cameron wrote:
> > G'day
> > My testing recently suggested that terrible the performance hit one
> > used to get with evaluate() has all but gone now.  So that's good.
> >
> > The only gripe I have with it is that people tend to over-use it: it's
> > seldom the correct answer to a question about dynamic variables or any
> > other situation where one's inclination might be to use it.
> >
> > You touch on one instance when it seems to be necessary to use it, and
> > I can think of a couple of others, but a lot of people seem to use it
> > every time things get slightly confusing to them.  These are perhaps
> > the same people #who# #put# #pound-signs# #around# #everything#, "just
> > in case".
> >
> > My position is this:
> > 1) if one can reasonably NOT use evaluate(), then don't.
> > 2) if one thinks "I need to use evaluate() for that", have another
> > small think about it before deciding it's the right answer.
> > 3) and, yes, sometimes it's unavoidable.  In these cases, there's no
> > need to feel like one is doing something wrong.
> >
> > I think it's a three-step process before settling on using it.
> >
>
>
> >
>


-- 
Cheers
Simon Haddon


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"cfaussie" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cfaussie?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to