Everything is a List in ColdFusion!

Ok, maybe not everything, but alot of things are. I've recently come across a several situations that reminded me how great lists are, particularly in a loosely typed language like ColdFusion. There are tons of things that can be treated like a list in ColdFusion. Here's a short list of some of my favorites:
  • Sentences are lists of words delimited by spaces.
  • Lines of a page are lists delimited by a Carriage Return/Line Feed (CRLF).
  • Paragraphs are lists delimited by a pair of CRLFs.
  • File paths are lists delimited by forward or backward slashes. Use listLast() to grab the filename or listFirst() to grab the drive letter (windows).
  • File names are lists delimited by a period. Use listLast() to get the file's extension.
  • Decimal numbers are lists delimited by a period. Use listFirst() or listLast() to separate out the whole number or fraction.
All in all there are 20 functions in ColdFusion that handle lists - beyond those 20 are more that create obvious lists such as structKeyList(), arrayToList() and getClientVariablesList() - and even more that create less obvious lists such as decimalFormat() (decimals are a list), getDirectoryFromPath() (paths are lists), dateFormat() or timeFormat() (often lists delimited by slashes or colons). Still not convinced that lists are all that and a bag of chips? Jeff Peters actually wrote an entire book exclusively on ColdFusion Lists, Arrays, & Structures. Ahh structs, how I love ColdFusion structs, but that's a whole other post all-together...

Comments (4)

Add Comment ]

Raymond Camden I too love lists. They make so many tasks simple. Your example of getting an extension is a great one. Sure you could do it by reserving the string, finding the first ., changing that position to len of string minus the initial position, then using the mid function - but man, ListLast just does it in one hit.

Now I will stress that listFunctions can be pretty slow. For your example of treating a file as a list - that is ok, but normally I'd use listToArray to translate the file and then I'd loop over the array. This can result in significant performance gains.
dave ross everything is gravy until you have a multi-character delimter. Then you either have to use the function on cflib, or java's String.split(). I think the latter is faster (plus it gives you a strongly typed array of strings, in case you need it).
Pete Freitag Don't forget about the GetFileFromPath function even easier than ListLast for getting the file name from a path.

But you post a good point, you can save a lot of time when you parse data as lists.
Ms. Dark Very impressive. You made alot of great points. I learn more on your blog, than The Wallstreet Journal. New Orleans also sounds like fun.

Add Comment ]

Post a comment





Leave this field empty: