CFLib.org – Common Function Library Project

arrayTrim()

Last updated January 31, 2012

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

 
Rated 0 time(s). Average Rating: 0

Description:
This UDF trims an array to a specified number of elements. Triming is from right to left by default.

Return Values:
Returns an array.

Example:

view plain print about
<cfset aPlayers = arrayNew(1)>

<cfset aPlayers[1] = "Rooney">
<cfset aPlayers[2] = "Berbatov">
<cfset aPlayers[3] = "Gigs">
<cfset aPlayers[4] = "Scholes">
<cfset aPlayers[5] = "Ronaldo">
<cfset aPlayers[6] = "Beckham">

<cfset aPlayers = arrayTrim(aPlayers,4)>

<cfdump var="#aPlayers#">

Parameters:

No arguments.

Full UDF Source:

view plain print about
<!---
 This method trims an array to the specified number of elements.
 
 @return Returns an array. 
 @author Tayo Akinmade (olusina@hotmail.com) 
 @version 1, January 31, 2012 
--->

<cffunction name="arrayTrim" access="public" returntype="array" output="false" hint="This method trims an array to the specified number of elements. Triming is from right to left by default">
    <cfargument name="aArray" type="array" required="true" hint="The array to be trimed">
    <cfargument name="iLength" type="numeric" required="true" hint="The length the array is to be trimmed to">
    <cfargument name="sTrimFrom" type="string" required="false" hint="Direction of the array trim. RIGHT->LEFT|R, LEFT-RIGHT|L" default="L">
    <cfscript>
        var stLocal                            = structNew();

        stLocal.aTrimmedArray                 = arguments.aArray;                                        // set trimmed array
        stLocal.iLoopIteration                = arrayLen(stLocal.aTrimmedArray)-arguments.iLength;     // get number of delete iterations

        // check if new length is less that the current length
        if(arguments.iLength lt  arrayLen(stLocal.aTrimmedArray)){
            for(stLocal.i = 1; stLocal.i lte stLocal.iLoopIteration;stLocal.i = stLocal.i + 1){
                // get index of array element to delete
                if(compareNoCase(arguments.sTrimFrom,"R"EQ 0){
                    stLocal.iDeleteIindex    =  arrayLen(stLocal.aTrimmedArray);
                }
                else{
                    stLocal.iDeleteIindex    = 1;
                }

                // delete array element
                arrayDeleteAt(stLocal.aTrimmedArray, stLocal.iDeleteIindex);
            }
        }

        return stLocal.aTrimmedArray;
    
</cfscript>
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

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

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson