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.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<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>
1<cfset states = ["Ohio","Michigan","California","Indiana","Florida"]>
2
3 <cfloop from="1" to="#arrayLen(states)#" index="x">
4 <cfif states[x] EQ "Ohio">
5 Ohio Found!<br>
6 <cfelse>
7 <cfoutput>#states[x]#</cfoutput><br>
8 </cfif>
9 </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.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<cfset states = ["Ohio","Michigan","California","Indiana","Florida"]>
<cfif listFind(arrayToList(states),"Ohio")>
Ohio Found!
</cfif>
1<cfset states = ["Ohio","Michigan","California","Indiana","Florida"]>
2
3 <cfif listFind(arrayToList(states),"Ohio")>
4 Ohio Found!
5 </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.
This entry was posted on May 12, 2007 at 5:32 PM and has received 1592 views. Comments 10 |
Print this entry.
#1 by Jason Holden on 5/12/07 - 6:29 PM
#2 by zac spitzer on 5/13/07 - 12:12 AM
creating a struct like my_struct[q_table.pk_id]=q_table.currentrow is quite handy as well
#3 by PaulH on 5/13/07 - 5:08 AM
if there was a measurable speed difference i'd jump on this bandwagon but there's pitfalls to that approach & no real gain.
#4 by Lola LB on 5/13/07 - 7:07 AM
#5 by Sammy Larbi on 5/13/07 - 10:00 AM
#6 by Dan on 5/13/07 - 11:39 AM
@Paul - Nice catch, the listFindNoCase is probably going to be used more!
#7 by Mark Mandel on 5/13/07 - 8:53 PM
states.contains("Ohio");
and hook into some Java goodness ;)
#8 by PaulH on 5/13/07 - 10:31 PM
#9 by Dan on 5/14/07 - 9:51 AM
#10 by Shaji on 5/14/07 - 11:57 PM
More useful tips at http://coldfused.blogspot.com/2007/01/extend-cf-na...