Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListLen Null Entries 1

Status
Not open for further replies.

DeZiner

Programmer
May 17, 2001
815
US
I have set: <cfset MyList = &quot;,,,,,&quot;>

Then use: listlen(MyList, &quot;,&quot;)

this returns 0 where as I need it to return 5.

Is there another function I should be using. Ultimately I will be using listgetat to retrive items and populate the DB. Some items may be null and that's ok. By problem is if I listgetat(MyList, 3) I get an error.

Thanks!


DeZiner
Never be afraid to try something new.
Remember that amateurs built the Ark.
Professionals built the Titanic
 
Yep... that's a &quot;feature&quot; of ColdFusion's list functions... it ignores null elements. It's fully documented in the docs for ListLen(), etc:

Code:
Note
Code:
ColdFusion ignores empty list elements; thus, a list that is defined as &quot;a,b,c,,,d&quot; is treated as a four element list.

It's pretty annoying, isn't it. I've had one or two situations where it came in handy, but most of the time it's not what I want to have happen.


The only way around it is to make sure that the &quot;null&quot; values aren't really null. Luckily, a simple space will do it. And luckily, you can simply use Replace() to do what you need.
Code:
<CFSET lstMyList = &quot;,,,,,&quot;>

<CFSET lstFixedList = Replace(lstMyList, &quot;,,&quot;, &quot;, ,&quot;, &quot;ALL&quot;)>

<CFOUTPUT>#ListLen(&quot;#lstFixedList#&quot;, &quot;,&quot;)#</CFOUTPUT>



-Carl
 
Then you can use
Code:
Trim()
to make the list element null again:
Code:
<CFLOOP list=&quot;#lstFixedList#&quot; index=&quot;whichElement&quot;>
    <CFOUTPUT>#Trim(whichElement)#<br /></CFOUTPUT>
</CFLOOP>


-Carl
 
csteinhilber,

Thanks a lot!!!!!

I keep a website handy of CFML functions and it said nothing about the null values, so I was stumped. This is perfect though!

DeZiner
Never be afraid to try something new.
Remember that amateurs built the Ark.
Professionals built the Titanic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top