How to check value in an Array using Coldfusion

coldfusion

I have the below code:

<cfset abcList = "*,B,b,A,C,a">
<cfset abcList=ListToArray(abcList,',')>

When I output 'abcList' then it is giving me a value but when I use the 'abcList' in <cfif> it's not working. Here is the code which is creating the problem:

 <cfoutput>
   #abcList[1]# <!---This is giving '*' as Correct o/p--->
   <cfif #abcList[1]# eq '*'> <!---Here its going in else--->
     list has * at first place
    <cfelse>
     * is not first
   </cfif>
 </cfoutput>

Any suggestions on what's wrong in my code?

Best Answer

You don't necessarily need to convert the list to an array. If you are starting from a list variable, you may use Coldfusion list functions to do the same thing without specifying the array conversion.

<cfset abcList = "*,B,b,A,C,a">
<cfif Compare(listGetAt(abcList, 1), '*') EQ 0>
    Match
<cfelse>
    No Match
</cfif>

Note that most of Coldfusion's string comparisons are not case sensitive. So if you need to test that 'B' is not the same as 'b', you will need to use the compare() function or else use one of the Regular Expression string functions. In this case, compare() returns 0 if string 1 equals string 2. If you do not need case sensitivity, then you may simplify further:

<cfset abcList = "*,B,b,A,C,a">
<cfif listGetAt(abcList, 1) EQ '*'>
    Match
<cfelse>
    No Match
</cfif>
Related Topic