Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

ListContains() Function

Guest
Oct 05, 2011 Oct 05, 2011

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

1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Oct 05, 2011 Oct 05, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 05, 2011 Oct 05, 2011

Why would they offer a "Delimiter" option for ListContains() if its going to totally ignore it?

I guess I will use ListFind().

Chuck

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Oct 05, 2011 Oct 05, 2011

Re-read Jason's response. It does not ignore the delimiter. You are just misunderstanding how ListContains works  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 07, 2011 Oct 07, 2011
LATEST

@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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources