Nylon Technology Presentation - Some Of The Forgotten Functions

Posted June 15, 2007 at 10:57 AM

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

Post Comment  |  Ask Ben  |  Permalink  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jun 15, 2007 at 11:16 AM // reply »
15 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 »
6,371 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?


Ron Alexander
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 »
6,371 Comments

@Ron,

Thanks for jumping in there; that is absolutely correct.


Jun 15, 2007 at 8:27 PM // reply »
15 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. :)


Niall O'Doherty
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 »
6,371 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 »
123 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 »
6,371 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 »
123 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 »
6,371 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 »
3 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 Comment  |  Ask Ben

Recent Blog Comments
Nov 7, 2009 at 5:53 PM
Ask Ben: Javascript String Replace Method
You can find here an advanced function that prepared with javascript replace function. This can make the first letters of words, sentences, lines and whatever you define automatically: http://www.m ... read »
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »