Skip to main content
Inspiring
July 21, 2010
Question

Help with ListLen()

  • July 21, 2010
  • 2 replies
  • 1859 views

I'm confused. In Adobe ColdFusion 8 documentation online it is said that ColdFusion ignores empty list elements; thus, the list "a,b,c,,,d" has four elements.

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_l_15.html

If I use ListLen on the following List:

a|b|c|d| | |g|h >> <CFSET TheList = "a|b|c|d| | |g|h">

<CFSET Total = "#ListLen(TheList, "|")#> I got: 7 instead of 6 as the result

So does ListLen function is counting my "Bar" as delimiter on that list OR ListLen function is counting the list elements (a,b,c,d,g,h)??????

I need a coldfusion function to count the "Bars" NOT the elements. Or is there CF function counting the elements but including empty elements.

On my test, ListLen is actually counting the Delimiter which is the "Bar" in my case, does it means the documentation is wrong?

if the documentation is correct and it s counting the element, I did not get that result. Please help!!!

This topic has been closed for replies.

2 replies

Inspiring
July 21, 2010

If I use ListLen on the following List:

a|b|c|d| | |g|h >> <CFSET TheList = "a|b|c|d| | |g|h">

<CFSET Total = "#ListLen(TheList, "|")#> I got: 7 instead of 6 as the result

That's strange.  Because I get 8, which is what I'd expect.

Whilst listLen() might ignore empty elements, you have no empty elements in that example there: " " (a space) is not the same as "" (an empty string).  You have two elements that contain spaces there, but you have no empty elements.

--

Adam

mega_LAuthor
Inspiring
July 21, 2010

OK, that make sense but you still have not answer the question.

To me, your explanation indicates that ListLen is returning total element on the list since all elements are not empty string. So, ColdFusion documentation is incorrect then?

ilssac
Inspiring
July 21, 2010

Yes, the list functions count the elements of the list, not the delimiters.

That is the way I have always read the documentation and how I would exect the function to work.

As Dan is pointing out, other functions might be the way to go for the task you apparently have at hand.

Inspiring
July 21, 2010

To count your Bars ( I call those pipes by the way), use reReplace to set a new variable that strips out everything except that character.  Then get the len of the new variable.

mega_LAuthor
Inspiring
July 21, 2010

My list was originally using tab as the delimiters and since I don't know how to count tab delimiters I used Replace function to replace all tab

delimiters to Pipe delimiter. I thought it would be easier for me to write ListLen(MyList, "|") rather than  ListLen(MyList, " ?? ")

Do you mind provide me with Code example on how to use rereplace function with tab? and also How can I replace the second attribute to

empty value?

ReReplace(MyList, ???? , "", All) the WhatString attribute values are different on each list element.

Inspiring
July 21, 2010

Tab is chr(9).  You probably had to know that to convert them all to pipes.

This page, http://www.regular-expressions.info/, will help you figure out the regex part of it.