Searching Arrays For Elements
Through open source, co workers and other just asking for help I get to see a lot of other peoples code. I believe it is a great learning tool that every developer should utilize. I know that if someone were to look at my code they could easily critique so please do not take this article the wrong way. One thing I see over and over is looping over an array to search for a specific value. I think that converting this array to a list provides a clear path to search the list using the listFind() method. Here is what I see a lot of and curious why you would search an array this way.
<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.
<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.
