Ask a Jedi: List question

Kyle asks:

I have a scheduling portion of my site, which allows the user to select a start time and duration (maximum of 2 hours) for a meeting. Here is a sample of the code to determine time slots that are available:

<cfset timeTaken = "8.5,9,9.5,10,12,12.5,15.5">
<cfset time ="8,8.5,9,9.5,10,10.5,11,11.5,12,12.5,13,13.5,14,14.5,15,15.5,16"> <cfset availTime = "">
<cfset notAvail = "">

<cfloop list="#time#" index="t">
<cfif not listContains(timeTaken,t)>

<cfset availTime = listAppend(availTime,t)>

<cfset notAvail = listAppend(notAvail, t)>

Time: #time# <p>
Time Taken: #timeTaken# <p>
Available Time: #availTime# <p>
Not Available: #notAvail#

There is a simple example to show you my problem. For some reason, 8 and 15 are not found in the loop so therefore are classified as not available, even though they are. Should I be using another list function instead of not listContains() ?

You know that saying about gut instincts? You were right. listContains is indeed the issue. ListContains will tell you if any of the list items contains your match, but you want a full match. If you switch to listFind (or listFindNoCase), your problem goes away. Frankly, I have yet to see a good need for listContains, and every code block I've seen using it really wanted listFind instead!

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA