开发者

Is there a function similar to ArrayFind from ColdFusion 9 in ColdFusion 8?

开发者 https://www.devze.com 2023-01-28 09:10 出处:网络
I was talking with another fellow programmer at work and we use ColdFusion. He is telling me simply look for a value in an array I have to do a whole loop? Is it true there is no开发者_如何学C functio

I was talking with another fellow programmer at work and we use ColdFusion. He is telling me simply look for a value in an array I have to do a whole loop? Is it true there is no开发者_如何学C function in ColdFusion 8 to simply look for a value in an Array?


arrayFind() doesn't exist in ColdFusion 8. However, you don't need to loop. There are two ways:

Take advantage of the fact that ColdFusion arrays implement the interface java.util.List:

<cfset valueToFind = 1>
<cfset array = [1,2,3]>
<!--- add one because CF does 1 based vs. Java 0 based arrays --->
<cfset position = array.indexOf(valueToFind) + 1> 

Use list operations:

<cfset valueToFind = 1>
<cfset array = [1,2,3]>
<cfset position = listFind(arrayToList(array), valueToFind)>

The first (Java List) method is faster.


AFAIK there is no documented arrayFind function in CF8. There is an arrayFind function at cflib.org that takes advantage of some undocumented java functionality. See also How do I find a value in an array?:

... ColdFusion arrays are an implementation of java lists (java.util.List). So all the Java list methods are available for CF arrays.

So to search an array all you need to do is add the appropriate method. For example, given this array:

<cfset arry = listToArray("tom, dick, harry, phred")>

You can do a find like this: <cfset findValue = arry.indexOf("harry")>

would return a 2 - the index value of harry in the array.

indexOf returns the index value of the item in the array.

NB: Be aware that unlike CF methods, the java method matches on both the value AND type. So searching for the number 1 (integer/double/etcetera) is NOT the same as searching for "1" (string). If you do not know exactly what object types you are dealing with, the results may surprise you.

Take these two examples:

<cfscript>
// numeric searches are sensitive to type
arry = listToArray("2,4,6");
writeOutput('<br>indexOf("6") =  '&  arry.indexOf("6"));
writeOutput('<br>indexOf( val(6) )=  '&  arry.indexOf(val(6)));

// string searches are case sensitive
arry = listToArray("tom,dick,harry,phred");
writeOutput("<br>indexOf(harry) =  "&  arry.indexOf("harry"));
writeOutput("<br>indexOf(HaRry) = "&  arry.indexOf("HaRry"));
</cfscript>

A search for "6" yields different results than val(6)

  • indexOf("6") = 2
  • indexOf( val(6) )= -1

.. and a string search for "harry" yields different results than "HaRry"

  • indexOf(harry) = 2
  • indexOf(HaRry) = -1

So when using java methods, know what you are getting ... and what you are not.


Being the author of the UDF you found, I'd like to point out that the ArrayFind UDF uses CF's underlying java foundations, I highly doubt that Adobe would change that fundamental aspect of CF. Also this is fully documented in Rupesh Kumar's (one of Adobe's CF programmers) blog at: http://coldfused.blogspot.com/2007/01/extend-cf-native-objects-harnessing.html

regards, larry

0

精彩评论

暂无评论...
验证码 换一张
取 消