CFLib.org – Common Function Library Project

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

Last updated July 13, 2005

Version: 1 | Requires: ColdFusion 5 | Library: DataManipulationLib

 
Rated 0 time(s). Average Rating: 0

Description:
Returns a slice of an array.

Return Values:
Returns an array.

Example:

view plain print about
<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:

view plain print about
<cfscript>
/**
 * 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;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

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

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

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

Ben Forta Ben Forta added
GetHostAddress
28 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