CFLib.org – Common Function Library Project

arraySlice(ary[, start][, finish])

Last updated July 13, 2005

author

Darrell Maples

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
Returns a slice of an array.

Return Values:
Returns an array.

Example:

<cfscript>
    a = arrayNew(1);
    a[1] = "this";
    a[2] = "is";
    a[3] = "the";
    a[4] = "best";
    a[5] = "udf";
    a[6] = "that";
    a[7] = "has";
    a[8] = "ever";
    a[9] = "been";
    a[10] = "written";
</cfscript>

<cfdump var="#a#">
<cfdump var="#arrayslice(a, 2, 3)#">
<cfdump var="#arrayslice(a, 7)#">
<cfdump var="#arrayslice(a)#">

Parameters:

Name Description Required
ary The array to slice. Yes
start The index to start with. Defaults to 1. No
finish The index to end with. Defaults to the end of the array. No

Full UDF Source:

/**
 * Slices an array.
 * 
 * @param ary      The array to slice. (Required)
 * @param start      The index to start with. Defaults to 1. (Optional)
 * @param finish      The index to end with. Defaults to the end of the array. (Optional)
 * @return Returns an array. 
 * @author Darrell Maples (drmaples@gmail.com) 
 * @version 1, July 13, 2005 
 */
function arraySlice(ary) {
    var start = 1;
    var finish = arrayLen(ary);
    var slice = arrayNew(1);
    var j = 1;

    if (len(arguments[2])) { start = arguments[2]; };
    if (len(arguments[3])) { finish = arguments[3]; };

    for (j=start; j LTE finish; j=j+1) {
        arrayAppend(slice, ary[j]);
    }
    return slice;
}

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