CFLib.org – Common Function Library Project

arrayFind(array, valueToFind)

Last updated April 30, 2008

Version: 2 | Requires: ColdFusion MX | Library: DataManipulationLib

 
Rated 2 time(s). Average Rating: 5.0

Description:
The arrayFind function performs a case sensitive search to find the index number of a value in a single dimension array using the java.util.List indexOf method. Because this function performs a case sensitive search, Harry or hArry etc., would not be found (if the item is harry). Also since java arrays start at 0, I modified the returned value to conform to CFMX array index values.

Return Values:
Returns a number, 0 if no match is found.

Example:

view plain print about
<cfset arry = listToArray("tom, dick, harry, phred")>

<cfset test = arrayFind(arry,"tom")>

returns a 1

<cfset test2 = arrayFind(arry,"Tom")>

returns a 0 (not found)

Parameters:

Name Description Required
array Array to search. Yes
valueToFind Value to find. Yes

Full UDF Source:

view plain print about
<!---
 The arrayFind function uses the java.util.list indexOf function to find an element in an array.
 v1 by Nathan Dintenfas.
 
 @param array      Array to search. (Required)
 @param valueToFind      Value to find. (Required)
 @return Returns a number, 0 if no match is found. 
 @author Larry C. Lyons (larryclyons@gmail.com) 
 @version 2, April 30, 2008 
--->

<cffunction name="arrayFind" access="public" hint="returns the index number of an item if it is in the array" output="false" returntype="numeric">

<cfargument name="array" required="true" type="array">
<cfargument name="valueToFind" required="true" type="string">

<cfreturn (arguments.array.indexOf(arguments.valueToFind)) + 1>
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
3 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson