Home > Articles > Mobile Application Development & Programming > HTML5 & Web Standards

  • Print
  • + Share This
From the author of

From the author of

Quick Rules for Using CFScript

Those two functions will be enough to get us started. Here are some quick rules about using CFSCRIPT:

  • You use two forward slashes for single-line comments:

    // I am a single line CFSCRIPT comment
  • Semicolons define the end of a statement, as in Java or JavaScript:

    Some statement here;
  • You can perform conditional logic as you otherwise would using <CFIF> and <CFELSE>. It takes the following structure in CFSCRIPT:

if(expression) statement [else statement];

Here is an example:

<cfscript>

if ( 1 is 1) WriteOutput("Equal");
else WriteOutput("Not equal");

</cfscript>

The above code says, "If 1 is equal to 1, output 'Equal' to the browser; otherwise, they are not equal, so output 'Not Equal' to the browser." Note that line breaks and spaces are ignored.

You perform variable assignment as you otherwise would using <CFSET>, like this:

MyVariable = SomeValue;

That's enough CFSCRIPT to get us started writing user-defined functions.

First, declare a <CFSCRIPT> block. Next, tell ColdFusion that you are creating an user-defined function by writing "function" followed by a space, and then the name for your new function. Immediately following the function name, put any arguments that you want your function to accept inside parentheses. Let's go slowly through the isPalindrome example in Listing 2; it looks long here, but it's mostly comments.

Listing 2: Creating the isPalindrome Function

<cfscript>
   // declare UDF and name it "isPalindrome". 
   // It will accept one argument.

 function isPalindrome(MyString){


   // create a new variable called RevString, which
   // will reverse the order of the characters in the 
   // passed string "myString"

 RevString = Reverse(MyString); {
 
// set up an if-else block. If the reversed 
// string matches the original string, 
// it is a palindrome

if (Find(RevString, MyString) is 1) 

// set the "theResult" variable. If the string is a 
// palindrome, set the value to "Your string 
// is a palindrome!"

   theResult = "Your string is a palindrome!";

   // the strings don't match, so set the 
   // "theResult" variable accordingly.
else
   theResult = "Your string is not a palindrome :(";
   }

   // expose the "theResult" variable
   return theResult;
 }
</cfscript>

That's it. The comments within the code should explain what's going on line by line. Now that you have defined your function, you can call it like any other function within <CFOUTPUT> (as long as you have defined or included this function before the point on your template when you call it):

<cfoutput>#isPalindrome("pop")#</cfoutput>

Let's put the new UDF into a self-posting form so that we can test it. Save the file as Palindrome.cfm and load it. (See Listing 3.)

Listing 3: Putting the isPalindrome() Function to Work

<html>
<head>
   <title>UDF</title>
</head>

<body>

<cfscript>
 function isPalindrome(string){

 RevString = Reverse(MyString);
 {
   if (Find(RevString, MyString) is 1) 
   theResult = "Your string is a palindrome!";
   else
   theResult = "Your string is not a palindrome :(";
   }
   return theResult;
 }

</cfscript>

<cfif isDefined("FORM.MyString")>

<!---we can call our custom function just like any other function---->

<cfoutput>#isPalindrome(FORM.MyString)#</cfoutput>

</cfif>
<br>
<br>
Please type a string, and I will tell you if it is a 
   palindrome or not:
<br>
<form action="palindrome.cfm" method="post" name="MyString">
   <input type="text" name="MyString" size="20"><br>
   <input type="submit" name="Submit">
</form>

</body>

Load palindrome.cfm in your browser and test it out. It's fun! There are obvious palindromes, such as "Mom," "Anna," and "Hannah." However, once you really put it to the test with longer palindromes, you come up against the limitation: isPalindrome() does not allow for any punctuation or spaces between words.

If you reverse the characters of "Race Car," the function will return false because of the space between the words, even though the letters reversed form a palindrome. The French palindrome "Engage le jeu que je le gagne" ("Begin the game so that I can win") fails, too, because it has not only spaces, but also a capital letter. Any string with punctuation fails, too.

So, let's fix it up. First, we can easily tackle the capital letter problem by inserting the LCase function into the string manipulator. LCase is a standard ColdFusion function that forces all characters in a string to lower case. It accepts one argument: the string to force into lower case.

We could modify the reverse string by nesting the LCase function inside the Reverse function, like this:

RevString = Reverse(LCase(mystring));

There is a better solution, however: using FindNoCase().

  • + Share This
  • 🔖 Save To Your Account