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)>
<cfelse>
<cfset notAvail = listAppend(notAvail, t)>
</cfif>
</cfloop><cfoutput>
Time: #time# <p>
Time Taken: #timeTaken# <p>
Available Time: #availTime# <p>
Not Available: #notAvail#
</cfoutput>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!
Archived Comments
Man, this is right on target. I can't tell you how many times early in my CF coding career I had to remind myself to use listfind() instead of listcontains().
And, maybe this is senility creeping in, but I still have to remind myself from time to time.
You know what I hate about lists, you cant use listAppend on an empty list (in order to create one from stracth dynamctly). I always end up making arrays.
@Rual: Eh? Of course you can.
<cfset l = "">
<cfoutput>#listAppend(l, "Ray")#</cfoutput>
That works just fine. The empty string is a list of 0 items, so it adds it as the first item.
I love lists in CF, but i do tend to always fall back on structs because they are faster and more flexible
I dont know why you always use RUAL as my name lol, but no that doesnt work
<cfset l = "">
<cfset listAppend(l, "Ray")>
<cfset listAppend(l, "Ray 2")>
<cfoutput>#l#</cfoutput>
Displays an empty string, that exact same example with an array works. In order to make it work I need to do this
<cfset l = "">
<cfset l = listAppend(l, "Ray")>
<cfset l = listAppend(l, "Ray 2")>
<cfoutput>#l#</cfoutput>
Because you did it wrong. listAppend _returns_ the list. It doesn't operate on it. You need to _save_ it.
<cfset l = listAppend(l, "Ray")>
It is different from array funcs, per the docs. :)
Yeah, but I dont see the "sense" in that function, it should be operating on the list, why would I want to append something to it and not have it saved automactly? (if the array does the same thing) they shouldnt be named exactly the same (my opinion tough)
Heh, well, opinion or not - you had said that you can't use listAppend on an empty list - which isn't true. I get you may not like that it doesn't work the same as arrayAppend, but - it _does_ work.
To be honest - I encounter FAR more developers who get tripped up by how arrayAppend works.
fair enough :)