arrayTrim()
Last updated January 31, 2012
Version: 1 | Requires: ColdFusion MX | Library: DataManipulationLib
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:
<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:
<!---
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>
Search CFLib.org
Latest Additions
Dave Anderson added
iniToStruct
20 day(s) ago
Dave Anderson added
deDupeArray
20 day(s) ago
Richard added
dice
22 day(s) ago
Isaac Dealey added
getRelative
a while ago
Top Rated
backupDatabase
Rated 5.0, 22 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)
highlightAndCrop
Rated 5.0, 4 time(s)