Skip to main content
Inspiring
November 18, 2010
Question

Simple List Problem

  • November 18, 2010
  • 4 replies
  • 884 views

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.
 

    This topic has been closed for replies.

    4 replies

    Inspiring
    November 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 match whole values only, then use ListFind() or ListFindNoCase().

    Participating Frequently
    November 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

    Inspiring
    November 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().

    NettlesDAuthor
    Inspiring
    November 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.

    Inspiring
    November 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

    NettlesDAuthor
    Inspiring
    November 18, 2010

    Prefect.

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

    WolfShade
    Legend
    November 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..

    ^_^