Count number of items in a cold fusion list (including blank or empty values)

Currently in Coldfusion the ListLen() function does NOT count empy list elements so a list "a,b,c,,,d" has only 4 elements. The function below counts all elements including empty list elements, so would therefore return 6 in the mentioned example.

//Count all items in a list (including blank values)
function ListItemCountAll(list)
{
 tempList = list;
 //check occurances in string
 while(find(",,",tempList) GT 0)
 {
  intInsertPoint = find(",,",tempList);
  tempList = insert("0", tempList, intInsertPoint);
 }
 //Check start string
 if(Mid(tempList,1,1) IS ",")
 {
  
  tempList = tempList & 0;
 }
 //Check end string
 if(Mid(Reverse(tempList),1,1) IS ",")
 {
  
  tempList = tempList & 0;
 }
 intCount = ListLen(tempList);
 return intCount; 
}

This entry was posted in Cold Fusion. Bookmark the permalink.

One Response to Count number of items in a cold fusion list (including blank or empty values)

  1. udip says:

    this doesn’t work when there is something like a,b,”c,e”,d. Counts this as 5 rather than 4.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s