Nylon Technology Presentation - Some Of The Forgotten Functions

Posted June 15, 2007 at 10:57 AM by Ben Nadel

Tags: ColdFusion, Work

This isn't really so much of a presentation as it just a collection of functions to discuss. I just went through the ColdFusion MX7 function list and picked out a bunch of the functions that I thought people (including myself) might not be aware of. Not all of these are useful, but as we are getting ready for ColdFusion 8, I thought we should really bone up on the current language so that knowledge isn't left behind.

Also, cut me some slack on this one, I had to come up with something to present at 8:15AM for a 9:00AM meeting :)


As ColdFusion continues to expand in functionality with new features and new tags being added with every release, it is easy to lose track of some of the lesser used functions. Just going through the list of ColdFusion MX7 functions, here is a list of methods that I have overlooked, but would be useful to commit to memory.

ArraySwap( array, position1, position2 )

Takes the given array and swaps the elements at the two indecies. According to the documentation, this is more efficient than using multiple CFSet tags.

DateConvert( conversion_type, date )

Converts local time to Coordinated Universal Time (UTC), or UTC to local time. The function uses the daylight savings settings in the executing computer to compute daylight savings time, if required.

Now() = {ts '2007-06-15 08:55:04'}
DateConvert( "local2Utc", Now() ) = {ts '2007-06-15 12:55:04'}
DateConvert( "utc2Local", Now() ) = {ts '2007-06-15 04:55:04'}

DecimalFormat( number )

Converts a number to a decimal-formatted string. This returns a number as a string formatted with two decimal places and a thousands separator.

DecimalFormat( 1234456789 ) = 1,234,456,789.00

FindOneOf( set, string [, start ] )

Finds the first occurrence of any one of a set of characters in a string, from a specified start position. The search is case-sensitive.

FindOneOf( "aeiou", "Badonkadonk" ) = 2

GenerateSecretKey( algorithm )

Gets a secure key value for use in the Encrypt function.

GenerateSecretKey( "AES" ) = j9wBAdWJje5wF/X7xj7+1w==
GenerateSecretKey( "BLOWFISH" ) = Sy2rmAeMyIq321CMn0hmJA==
GenerateSecretKey( "DES" ) = EwKh8Vienoo=
GenerateSecretKey( "DESEDE" ) = 09wTmPdAxEUICy+/es5XAvJtrTFGzTIy

AES: the Advanced Encryption Standard specified by the National Institute of Standards and Technology (NIST) FIPS-197.
BLOWFISH: the Blowfish algorithm defined by Bruce Schneier.
DES: the Data Encryption Standard algorithm defined by NIST FIPS-46-3.
DESEDE: the "Triple DES" algorithm defined by NIST FIPS-46-3.

You cannot use the GenerateSecretKey function to generate a key for the ColdFusion default encryption algorithm (CFMX_COMPAT) of the Encrypt and Decrypt functions.

ListContains[NoCase]( list, substring [, delimiters ] )

Determines the index of the first list element that contains a specified substring.

ListContains( "Hey,There,Hot,Momma", "mom" ) = 4

ListValueCount( list, value [, delimiter ] )

Counts instances of a specified value in a list. The search is case-sensitive.

ListValueCount( "2,1,2,6,9,1,1,1,3,4", "1" ) = 4

ParagraphFormat( string )

Replaces characters in a string: Single newline characters (CR/LF sequences) with spaces, double newline characters with HTML paragraph tags.

HtmlEditFormat( ParagraphFormat( "Maria Bello" & Chr( 13 ) & Chr( 10 ) & "is Mad Hot" & Chr( 13 ) & Chr( 10 ) & Chr( 13 ) & Chr( 10 ) & "Word" ) ) =
Maria Bello is Mad Hot <P> Word <P>

Note: This function is good if you don't care about web standards ;)

RemoveChars( string, start, count )

Removes characters from a string.

RemoveChars( "Like, what's up?", 1, 6 ) = what's up?

RepeatString( string, count )

Creates a string that contains a specified number of repetitions of the specified string.

RepeatString( "N-", 5 )Nice = N-N-N-N-N-Nice

Reverse( string )

Reverses the order of items, such as the characters in a string, the digits in a number, or the elements in an array.

Reverse( "Naomi" ) = imoaN

Note: I could not get this to work with an array.

StripCR( string )

This strips the carriage returns out of a string.

Find( Chr( 13 ), StripCR( "Nice#Chr( 13 )##Chr( 10 )#Pants" ) ) = 0
Find( Chr( 10 ), StripCR( "Nice#Chr( 13 )##Chr( 10 )#Pants" ) ) = 5
Len( StripCR( Chr( 13 ) & Chr( 10 ) ) ) = 1




Reader Comments

Jun 15, 2007 at 11:16 AM // reply »
16 Comments

Hey I do get to use some of the functions outlined here especially ParagraphFormat and GenerateSecretkey.

And you're right about brushing up on ColdFusion MX7 before venturing into ColdFusion 8. Infact, I started reading my ColdFusion MX 7 documentation to refamiliarize myself with features and functionality I hardly or never used.


Jun 15, 2007 at 11:18 AM // reply »
11,243 Comments

It's funny, I didn't even know there was a GenerateSecretKey() function. It looks cool. I will have to play around with it some.


Jun 15, 2007 at 3:13 PM // reply »
11 Comments

Thanks for the refresher. One line in particular troubles me:

Find( Chr( 10 ), StripCR( "Nice#Chr( 13 )##Chr( 10 )#Pants" ) ) = 5

Why does it find 5 line feeds in that string?


Jun 15, 2007 at 4:20 PM // reply »
2 Comments

It returns 5 because it strips out the Carriage Return, Chr(13), and is left with "Nice#Chr(10)#Pants" so it FINDS the Chr(10) in the fifth position. Chr(10) does not get stripped because it is a line feed.


Jun 15, 2007 at 5:50 PM // reply »
11,243 Comments

@Ron,

Thanks for jumping in there; that is absolutely correct.


Jun 15, 2007 at 8:27 PM // reply »
20 Comments

The really useful one I'd forgotten about until recently is ValueList(). Saves all that unnecessary query looping!!


Jun 16, 2007 at 12:00 AM // reply »
11 Comments

Brain fart! I was thinking 5 represented a count. Index never crossed my mind. Thanks. :)


Jun 16, 2007 at 7:49 AM // reply »
1 Comments

One of the smaller functions I like is the YesNoFormat() function.

YesNoFormat(1): Yes
YesNoFormat(0): No

Nice for display purposes instead of writing CFIF CFELSE for each condition!

Niall.


Jun 16, 2007 at 7:52 AM // reply »
11,243 Comments

@Niall,

That is a fantastic function. It is also super useful when setting the "null" attribute of the CFQueryParam tag:

<cfqueryparam
value="#date#"
cfsqltype="CF_SQL_TIMESTAMP"
null="#YesNoFormat( NOT IsDate( date ) )#"
/>


Jul 17, 2007 at 1:12 AM // reply »
132 Comments

@Ben

Using YesNoFormat() like that is never required.

<cfqueryparam
value="#date#"
cfsqltype="CF_SQL_TIMESTAMP"
null="#NOT IsDate( date )#"
/>

Would have been fine.

In fact, null="0", null="1", null="true", null="yes", null="no", null="false", null="#not 1#", and any other variant you can think of is fine too.

This is true for all boolean attributes. The CF runtime will convert boolean values for you, and expressions that evaluate to booleans never need to be specially converted.

A fun example is:
<cfoutput>#true eq "yes" eq (not isDate("foo")) eq 1#</cfoutput> == YES

The only special case behavior that YesNoFormat() has is turning an empty string into false which is honestly more clearly written as #not len(str)#, and the fact that it generates "Yes" and "No" instead of "YES" and "NO" like the regular string converted result of a boolean expression.

YesNoFormat() is really not useful at all unless you're outputting to the page.

The function is actually equivalent to:

function YesNoFormat( value ) {
if( not len(value) or not value ) return "No";
if( value ) return "Yes";
}

The CF documentation makes this very confusing, since there is no YES or NO keyword, only true and false, but the documentation uses the words "yes" and "no" when describing boolean attributes, and the runtime outputs YES and NO when you try to print a boolean expression, but it outputs "true" and "false" if you output the keywords true and false directly!

Crazy stuff eh?


Jul 17, 2007 at 7:03 AM // reply »
11,243 Comments

@Elliott,

Have you tested that to actually work? I understand that 1 = Yes = true = etc... However, I think with the List and Null attributes of the CFQueryParam tag, it actually requires a Yes/No value. I am like 99% sure it will break otherwise... certainly, I would not use the YesNoFormat() in that case as it only adds extra typing.


Jul 17, 2007 at 12:07 PM // reply »
132 Comments

I'm 100% sure it'll work in any tag, including <cfuqueryparam> and it's null attribute.

This is true from CFMX 6+, but not for CF5 when it didn't allow an arbitrary expression in pound signs, so then you did need yesNoFormat().

http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:30658
Also confirms this.


Jul 17, 2007 at 12:17 PM // reply »
11,243 Comments

@Elliott,

I have to say that I just tested this and you are 100% correct. I can swear that I have seen this not work, and I have been using MX7 for years. This awesome! Thanks you so much for pointing out the error of my ways! I have been so sick an tired of using the YesNoFormat()!

Thou dost rocketh.


Dec 21, 2007 at 10:31 AM // reply »
5 Comments

I just found a great use for findoneof(). When using verity indexing, sometimes you get coldfusion code in the context result. You can avoid this by using the spider, but that's another issue.

I couldn't get find() or refind() to work when searching for #, but this works great:

<cfif findoneof("##",stringtoSearch)>might be code</cfif>



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
May 23, 2013 at 9:55 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dan, According to the CF Admin, I'm running Java "1.6.0_45". As far as the DB column, in the database it's an INT. I'll see if I can dig into what CF sees it as. @WebManWalking, But h ... read »
May 23, 2013 at 9:49 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, I think the problem is that we're used to loose typing in ColdFusion, like JavaScript. If a value is a number but it's needed in an expression to be a string, noooo problem. I've encountered ... read »
May 23, 2013 at 9:47 AM
ColdFusion QueryAppend( qOne, qTwo )
You rock! Thank you, thank you, thank you!!! ... read »
May 23, 2013 at 5:19 AM
Ask Ben: Print Part Of A Web Page With jQuery
How to print also the background color of table cells and table lines ... read »
May 23, 2013 at 3:55 AM
Javascript Array Methods: Unshift(), Shift(), Push(), And Pop()
very interesting and helpful too. ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools