– 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

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

Return Values:
Returns an array.


view plain print about
<cfset myArray = ArrayNew(1) />
<cfloop index="i" from="1" to="100">
    <cfset myArray[i] = i />
<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#">


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 ( 
 @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 />
    <cfset finish = arguments.start + arguments.length - 1 />

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


Latest Additions

CF Ninja CF Ninja added
a while ago

Stephen Withington Stephen Withington added
a while ago

Adam Cameron Adam Cameron added
a while ago

Ray Ford Ray Ford added
a while ago

Top Rated

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

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 9 time(s)

Darwan Leonardo Sitepu splitNumber
Rated 5.0, 9 time(s)

Created by Raymond Camden / Design by Justin Johnson