Copy link to clipboard
Copied
I have a table with a column for listing colors.
The current comma delimited color list is: Orange & White, White
When adding a new color, I verify the color being added does not already exists. So I do a simple ListContains() function like this:
<CFSET v.vendorcolors = "Orange & White, White">
<CFSET imprintcolor = "Orange">
<CFIF ListContains(v.vendorcolors,imprintcolor,",")>
<CFSET message = "This color already exists.">
<CFELSE>
<CFSET form.vendorcolors = "#ListAppend(v.vendorcolors,imprintcolor)#">
</CFIF>
As you can see, if it doesn't exist in the list, then I APPEND it to the list.
Now, this was working great until I tried to add "ORANGE" as color.
You can see I have a "Orange & White" color options, but not just "Orange".
When I run this IF statement above, it says the list already contains the color "Orange". If I am telling it to use a comma as the delimiter, then why is it saying it exists?
Chuck
Copy link to clipboard
Copied
You want to use ListFind(). ListContains() is a wildcard search. When you use ListContains() you are searching any values for *Orange* so it will return true for "Orange", "Orange & White", "Orangeapotomus", "Orange Julius", "Orange you glad I didn't say Banana?", and anything with "Orange" in it. Note the casing. You might also want to look at ListFindNoCase().
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6d84.html
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6d80.html
Copy link to clipboard
Copied
Why would they offer a "Delimiter" option for ListContains() if its going to totally ignore it?
I guess I will use ListFind().
Chuck
Copy link to clipboard
Copied
Re-read Jason's response. It does not ignore the delimiter. You are just misunderstanding how ListContains works
Copy link to clipboard
Copied
@cwmcquire: You might also consider whether "Orange" is going to be considered the same as "ORANGE"... if they are in fact considered to be the same thing, you'll probably want to use ListFindNoCase().
In addition, you can simplify your assignment statement to just
<cfset form.vendorcolors = ListAppend(v.vendorcolors, imprintcolor)>
Note the absence of the unneeded/redundant quotes and crunches.
--
/ron