Nylon Technology Presentation - Some Of The Forgotten Functions
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,Silly,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 Talented" & Chr( 13 ) & Chr( 10 ) & Chr( 13 ) & Chr( 10 ) & "Word" ) ) =
Maria Bello is Mad Talented <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
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.
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.
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?
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.
@Ron,
Thanks for jumping in there; that is absolutely correct.
The really useful one I'd forgotten about until recently is ValueList(). Saves all that unnecessary query looping!!
Brain fart! I was thinking 5 represented a count. Index never crossed my mind. Thanks. :)
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.
@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 ) )#"
/>
@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?
@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.
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.
@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.
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>