Searching Arrays For Elements
Saturday May 12, 2007 4:32 PM
Word Count: 282
<cfset states = ["Ohio","Michigan","California","Indiana","Florida"]>
<cfloop from="1" to="#arrayLen(states)#" index="x">
<cfif states[x] EQ "Ohio">
Ohio Found!<br>
<cfelse>
<cfoutput>#states[x]#</cfoutput><br>
</cfif>
</cfloop>
<cfloop from="1" to="#arrayLen(states)#" index="x">
<cfif states[x] EQ "Ohio">
Ohio Found!<br>
<cfelse>
<cfoutput>#states[x]#</cfoutput><br>
</cfif>
</cfloop>
The code above will run fine when executed I just do not see it as a clear solution. While this example is not the best it does provide some code. Also the example uses Scorpio Implicit array creation so dont let that throw you off.
<cfset states = ["Ohio","Michigan","California","Indiana","Florida"]>
<cfif listFind(arrayToList(states),"Ohio")>
Ohio Found!
</cfif>
I am curious to find out others thoughts on this. This is how I have always searched an array and found it cleaner that looping an array.
<cfif listFind(arrayToList(states),"Ohio")>
Ohio Found!
</cfif>
