Search code examples
coldfusioncfif

Using ColdFusion, how do I replicate a compare statement with listFind?


I'm setting up something to allow me to masquerade as another user in my system. But, I want to use listFind instead of a compare() method.

<cfif compare(session.userName, "userOne") EQ 0>
  <cfset #session.userName# = "userThree">
</cfif>

In the above statement, I'm trying to conform it to a listFind, where if userOne is currently logged in, then set the session.userName to userThree. But, I'm having trouble.

What I have thus far...

<cfif #ListFind("userOne, UserTwo")#>
  <cfset #session.userName# = "userThree">
</cfif>

Solution

  • You will need to provide two independent arguments to ListFind. At the moment you are providing one string. The list is also the first arguments and string search for is the second.

    <cfif ListFind("userOne", session.userName)>
      <cfset session.userName = "userThree">
    </cfif>
    

    On a side note, the hashes are only necessary for string interpolation. In other words, only when you want a variable/expression to be evaluated and inserted into a string.