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

Simple List Problem

Explorer ,
Nov 18, 2010 Nov 18, 2010

This should be simple but I don't see it.  I'm sure there is a work around or a better way to do it but I can't see to find it.

Problem:  The check boxes will be checked if it finds the number 1, 4, 6 or 14

What should happen:  The check boxes for 6 and 14 should only be checked because the list contains the number 6 and 14 and not 1, 4, 6 and 14.

<cfset AuditionCategory = ListAppend(AuditionCategory, 6, ",")>

<cfset AuditionCategory = ListAppend(AuditionCategory, 14, ",")>

<cfoutput>#AuditionCategory#</cfoutput>

6,14

<cfloop from="1" to="20" index="i">
  <input type="checkbox" name="test" value="#i#
  <cfif listContains(AuditionCategory, i, ",")>checked="checked"</cfif> />
</cfloop>

What is happening is #1, #4, #6 and #14 are being checked instead of just #6 and #14.
For some reason, it is finding 1, 4, 6 and 14 instead of just finding 6 and 14.

Can anyone tell me what the solution to this problem is?  Why is it trying to break apart the number 14 as 1 and 4.
 

818
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 ,
Nov 18, 2010 Nov 18, 2010
Just replace your "ListContains" function with "ListFind"

Like this,

<cfloop from="1" to="20" index="i">
  <input type="checkbox" name="test" value="#i# 
  <cfif ListFind(AuditionCategory, i, ",")>checked="checked"</cfif> />
</cfloop>

HTH

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 ,
Nov 18, 2010 Nov 18, 2010

Prefect.

What's the difference?  ListFind can do numbers and strings versus ListContains only supports strings?  Just wanting to know.

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
LEGEND ,
Nov 18, 2010 Nov 18, 2010

I believe that ListFind treats each list element of the substring as comparative, whereas ListContains treats each character of the list element as a comparative.

ICBW..

^_^

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
Contributor ,
Nov 18, 2010 Nov 18, 2010

ListContains() determines whether any element in the list Contains the

value you're searching. ListFind() determines whether any element in the

list IS the value you're searching.

list1 = "Apple,Brown,Cow";

list2 = "A,B,C";

ListFind(list1, "A"); --> false, because there is no element "A"

ListFind(list2, "A"); --> true

ListContains(list1, "A"); --> true, because "Apple" contains "A"

ListContains(list2, "A"); --> true

ListContains(list1, "w"); --> true, because "Cow" contains "w"

ListContains(list2, "w"); --> false, because there is no element "w"

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 ,
Nov 18, 2010 Nov 18, 2010

> For some reason, it is finding 1, 4, 6 and 14 instead of
> just finding 6 and 14.

That  is what ListContains() does. It searches for the value +anywhere+ in the list element. So searching for "1" would match "1", "11", "14",  "114", etcetera. If you wish to find whole values only (ie exact match) , use ListFind() or ListFindNoCase().

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 ,
Nov 18, 2010 Nov 18, 2010

I see.  I went back and looked at the documentation again and it was all there.  I must have missed that while trying to find a solution.

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
Contributor ,
Nov 18, 2010 Nov 18, 2010

You want ListFind() = match an entire element, not ListContains() which is

finding "4" in "14" and "1" in "14".

<cfloop from="1" to="20" index="i">

checked="checked" />

</cfloop

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 ,
Nov 18, 2010 Nov 18, 2010
LATEST

For some reason, it is finding 1, 4, 6 and 14 instead of

just finding 6 and 14.

That is what ListContains() does. It searches for the value anywhere in the list element. So searching for "1" would match "1", "11", "14", "114", etcetera. If you wish to match whole values only, then use ListFind() or ListFindNoCase().

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