Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default ListFInd

    Hi, Im trying to find info in one list and see if another list contains any of
    those numbers. Like this: I have a var called #allusers# and allusers contains
    a list of this '5,7,17'. These are security levels. next i have a use that has
    a var called #session.userlevels# and session.userlevels contains '5,7'. So i
    want to see if #session.userlevels# contains ANY of the numbers in the
    #allusers# list. Know what i mean? I have tried listfind and listcontains and i
    cant seem to get them to work. This is my simple code that i thought would
    work: <cfif ListContains(session.userlevels, '#allusers#', ',')>. But it does
    not. Any help would be appreciated. Thanks Tony Paolillo

    TonyP Guest

  2. #2

    Default Re: ListFInd

    First off, ListContains compares each list element with a substring, so if
    YourList = 5,7,17, ListContains(YourList, "1") will be TRUE. You don't want
    that.

    The best approach is to use some approach shown below:



    <CFSET ok = FALSE>
    <CFLOOP LIST="#session.userLevels#" INDEX="i">
    <CFIF ListFind(allUsers, i)>
    <CFSET ok = TRUE>
    <CFBREAK>
    </CFIF>
    </CFLOOP>

    <CFIF ok>
    --- user authenticates ---
    </CFIF>

    jdeline Guest

  3. #3

    Default Re: ListFInd

    When in doubt, roll your own... (Just tweak it a bit if you want a list of the
    matching values returned rather than just T/F)

    <cffunction name="listFindAny" access="private" output="no"
    returnType="boolean">
    <!--- Determine if any of the values in list2 exist in list1 --->
    <cfargument name="list1" type="string" required="yes">
    <cfargument name="list2" type="string" required="yes">

    <cfset foundElement = false>
    <cfloop list="#list2#" index="listElem">
    <cfif listFind (list1, listElem) is not 0>
    <cfset foundElement = true>
    <cfbreak>
    </cfif>
    </cfloop>

    <cfreturn foundElement>
    </cffunction>

    <cfoutput>
    Item Found: #listFindAny ("5,7,17", "5,7")#<br>
    No Matches: #listFindAny ("5,7,17", "3,8")#<br>
    </cfoutput>

    Iceborer Guest

  4. #4

    Default Re: ListFInd

    Thanks guys, its works perfectly Here is the code i used: <CFSET
    session.alluseraccess='FALSE'> <CFLOOP LIST='#session.userlevels#'
    INDEX='i'> <CFIF ListFind(allUsers, i)> <CFSET
    session.alluseraccess = 'TRUE'> <CFBREAK> </CFIF>
    </CFLOOP> Thanks Again Tony

    TonyP Guest

  5. #5

    Default Re: ListFInd

    Thanks guys, its works perfectly Here is the code i used: <CFSET
    session.alluseraccess='FALSE'> <CFLOOP LIST='#session.userlevels#'
    INDEX='i'> <CFIF ListFind(allUsers, i)> <CFSET
    session.alluseraccess = 'TRUE'> <CFBREAK> </CFIF>
    </CFLOOP> Thanks Again Tony

    TonyP Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139