CFLib.org – Common Function Library Project

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

Last updated June 11, 2009

author

G.Arlington

Version: 0 | Requires: CF6 | Library: DataManipulationLib

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

Return Values:
Returns an array.

Example:

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

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

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson