CFLib.org – Common Function Library Project

ListFix(list [, delimiter] [, null])

Last updated July 31, 2004

Version: 3 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 3 time(s). Average Rating: 5.0

Description:
By default, ColdFusion will ignore empty elements in a list. This can be a problem if you want to treat empty elements as null entries. For example, the list "Goo,Foo,,Moo" is considered a 3 item list, since the ",," entry is ignored. ListFix will take these entries and replace them with a null character of your choosing. Version 3 now correctly handles multiple delimiters.

Return Values:
Returns a list.

Example:

view plain print about
<cfoutput>
<cfset l = "raymond,camden,,goober,pile">
#listFix(l)#<br>
<cfset l2 = "raymond,camden,,goober,pile,">
#listFix(l2)#<br>
<cfset l3 = "@raymond@camden@@goober@pile">
#listFix(l3,"@","_null_")#<br>
</cfoutput>

Parameters:

Name Description Required
list The list to parse. Yes
delimiter The delimiter to use. Defaults to a comma. No
null Null string to insert. Defaults to "NULL". No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Fixes a list by replacing null entries.
 * This is a modified version of the ListFix UDF 
 * written by Raymond Camden. It is significantly
 * faster when parsing larger strings with nulls.
 * Version 2 was by Patrick McElhaney (pmcelhaney@amcity.com)
 * 
 * @param list      The list to parse. (Required)
 * @param delimiter      The delimiter to use. Defaults to a comma. (Optional)
 * @param null      Null string to insert. Defaults to "NULL". (Optional)
 * @return Returns a list. 
 * @author Steven Van Gemert (pmcelhaney@amcity.comsvg2@placs.net) 
 * @version 3, July 31, 2004 
 */

function listFix(list) {
var delim = ",";
  var null = "NULL";
  var special_char_list      = "\,+,*,?,.,[,],^,$,(,),{,},|,-";
  var esc_special_char_list  = "\\,\+,\*,\?,\.,\[,\],\^,\$,\(,\),\{,\},\|,\-";
  var i = "";
       
  if(arrayLen(arguments) gt 1) delim = arguments[2];
  if(arrayLen(arguments) gt 2) null = arguments[3];

  if(findnocase(left(list, 1),delim)) list = null & list;
  if(findnocase(right(list,1),delim)) list = list & null;

  i = len(delim) - 1;
  while(i GTE 1){
    delim = mid(delim,1,i) & "_Separator_" & mid(delim,i+1,len(delim) - (i));
    i = i - 1;
  }

  delim = ReplaceList(delim, special_char_list, esc_special_char_list);
  delim = Replace(delim, "_Separator_""|""ALL");

  list = rereplace(list, "(" & delim & ")(" & delim & ")""\1" & null & "\2""ALL");
  list = rereplace(list, "(" & delim & ")(" & delim & ")""\1" & null & "\2""ALL");
      
  return list;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
3 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson