Searching Arrays For Elements

Word Count: 282

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.

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

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.

Comments

#1 Posted By: Jason Holden Posted On: 5/12/07 6:29 PM
I like the shorter syntax in example 2!
#2 Posted By: zac spitzer Posted On: 5/13/07 12:12 AM
I usually convert the list to a struct and using structkeyexists... if you are doing it as a once off it there's not much difference, but if you are going to be searching the same list often it rocks...

creating a struct like my_struct[q_table.pk_id]=q_table.currentrow is quite handy as well
#3 Posted By: PaulH Posted On: 5/13/07 5:08 AM
as far as i can tell there's no real speed gain using ListFind() vs simply looping over the states array. while you write less code w/the ListFind() technique i think it's a bit harder to understand at first glance. in any case, ListFind() is case sensitive (the looping method isn't, at least on windows) so you'd need ListFindNoCase() *and* it won't work if you're searching for multiple values w/out some hoop jumping.

if there was a measurable speed difference i'd jump on this bandwagon but there's pitfalls to that approach & no real gain.
#4 Posted By: Lola LB Posted On: 5/13/07 7:07 AM
Actually, i disagree, PaulH. The 2nd example is a lot easier to read. Valid point about searching for multiple values, though.
#5 Posted By: Sammy Larbi Posted On: 5/13/07 10:00 AM
Well, I like the idea in the 2nd example, but you'd need to know in advance that none of your data items contain the delimiter. Depending on what you are storing and what you need it for, I like the idea of using a struct as the mechanism as well.
#6 Posted By: Dan Posted On: 5/13/07 11:39 AM |
Author Comment
Maybe as always it just comes down to situation. I guess I can see a need for both now. I still think the 2nd is easier to read.

@Paul - Nice catch, the listFindNoCase is probably going to be used more!
#7 Posted By: Mark Mandel Posted On: 5/13/07 8:53 PM
You could also do:
states.contains("Ohio");
and hook into some Java goodness ;)
#8 Posted By: PaulH Posted On: 5/13/07 10:31 PM
now that's "readable", no need to parse the code & the meaning's not easily lost. but contains() is case-sensitive too.
#9 Posted By: Dan Posted On: 5/14/07 9:51 AM |
Author Comment
Mark, Great tip. Anymore useful tips for tapping into Java?
#10 Posted By: Shaji Posted On: 5/14/07 11:57 PM


Post Your Comment







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.