Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.
-
TonyP #1
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
-
jdeline #2
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
-
Iceborer #3
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
-
TonyP #4
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
-
TonyP #5
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



Reply With Quote

