tags:

views:

199

answers:

3

I have below code

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

When I Printed 'abcList' then it sis givig me value but when i use the 'abcList' in <cfif> its not working, Below is the code which created 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 whats wrong in my code?

Thanks,

+2  A: 
<cfset abcList = "*,B,b,A,C,a">
<cfset abc=ListToArray(abcList)>

<cfif #abc[1]# eq "*">OK<cfelse>FAIL</cfif>

<cfif abc[1] eq "*">OK<cfelse>FAIL</cfif>

Prints "OK OK" for me. Can you re-confirm it prints something else for you?

Tomalak
+2  A: 

It also works fine for me. Perhaps you have some extra spaces in the list values? That would skew the results:

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

<cfoutput>
   The value of abcList[1] = #abcList[1]# <br/>
   <cfif abcList[1] eq '*'> 
     list has * at first place
  <cfelse>
     The else condition was hit because abcList[1] is "(space)*" and not just "*" 
   </cfif>
 </cfoutput>

Try trimming the value first. Also, the # signs around the value are not needed.

   <cfif trim(abcList[1]) eq '*'>
       .... 
   </cfif>

If that does not work, display the ascii values of both characters. Perhaps they are different than you are thinking.

<cfoutput>
   ASCII abcList[1] = #asc(abcList[1])# <br/>
   ASCII "*" = #asc("*")# <br/>
</cfoutput>
Leigh
Trimming is worked fine, I think there is some Space problem with my code, Thnks Leigh. now i am able to compare without # also
CFUser
This is why you always should post code that *actually fails*, instead of code you've just made up that you *think* resembles your problem.
Tomalak
+1  A: 

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>
Dan Sorensen