CFLib.org – Common Function Library Project

arraySlice2(thisArray [, start] [, length])

Last updated June 11, 2009

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

 
Rated 2 time(s). Average Rating: 5.0

Description:
An arraySlice using Java 1.4 ArrayList built-in method subList(int from, int to).

Return Values:
Returns an array.

Example:

view plain print about
<cfset myArray = ArrayNew(1) />
<cfloop index="i" from="1" to="100">
    <cfset myArray[i] = i />
</cfloop>
<cfdump var="#myArray#">
<cfset myArray2 = arraySlice2(myArray) />
<cfdump var="#myArray2#">
<cfset myArray2 = arraySlice2(myArray, 90) />
<cfdump var="#myArray2#">
<cfset myArray2 = arraySlice2(myArray, 99, 10) />
<cfdump var="#myArray2#">

Parameters:

Name Description Required
thisArray Array to slice. Yes
start Starting value (defaults to 1). No
length Length of slice (defaults to 0 which will return the entire rest of the items after the start value). No

Full UDF Source:

view plain print about
<!---
 An arraySlice using Java 1.4 ArrayList built-in method.
 
 @param thisArray      Array to slice. (Required)
 @param start      Starting value (defaults to 1). (Optional)
 @param length      Length of slice (defaults to 0 which will return the entire rest of the items after the start value). (Optional)
 @return Returns an array. 
 @author G.Arlington (germann_arlington@yahoo.co.uk) 
 @version 0, June 11, 2009 
--->

<cffunction name="arraySlice2" returntype="array" output="false">
    <cfargument name="thisArray" required="true" type="array" />
    <cfargument name="start" required="false" type="numeric" default="1" />
    <cfargument name="length" required="false" type="numeric" default="0" />
    <cfset var resArray = createObject("java""java.util.ArrayList").Init(arguments.thisArray) />
    <cfset var thisArrayLen = ArrayLen(arguments.thisArray) />
    <cfset var finish = 0 />
    <cfif (arguments.length EQ 0) OR ((arguments.start + arguments.length - 1) GT thisArrayLen)>
        <cfset arguments.length = thisArrayLen - arguments.start + 1 />
    </cfif>
    <cfset finish = arguments.start + arguments.length - 1 />

    <cfreturn resArray.subList(JavaCast("int", arguments.start - 1), JavaCast("int", finish)) />
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Troy Pullis Troy Pullis added
firstXDayOfMonth
16 day(s) ago

Henry Ho Henry Ho added
arrayMap
29 day(s) ago

Henry Ho Henry Ho added
queryGetRow
30 day(s) ago

Tony Felice Tony Felice added
getRowFromQuery
30 day(s) ago

Top Rated

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

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 8 time(s)

Markus Schneebeli                                 ListRemoveByStri...
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson