CFLib.org – Common Function Library Project

arrayCompact(arr [, delim])

Last updated March 2, 2007

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

 
Rated 0 time(s). Average Rating: 0

Description:
This function is designed to remove undefined postions from an array. Will not work in CF5, and would not recommend using it for large arrays. Optional argument to pass in delimiter (default is Pipe symbol).

Return Values:
Returns an array.

Example:

view plain print about
<!--- create the array --->
<cfset tmp=arraynew(1)>
<cfset tmp[1]="a">
<cfset tmp[3]="c">
<cfset tmp[4]="d">
<!--- let's see it in all it's un-glory --->
<cfdump var="#tmp#" label="original array">
<!--- compacting - grindgrindgrind --->
<cfset newTmp=arrayCompact(tmp)>
<!--- show me --->
<cfdump var="#Newtmp#" label="New Compacted array">
<!--- now, the pudding --->
<cfoutput>
Looping new Array<br />
<cfloop from="1" to="#arraylen(newTmp)#" index="i">
index #i# = #newTmp[i]#<br />
</cfloop>
</cfoutput>

Parameters:

Name Description Required
arr Array to compact. Yes
delim Temporary list delimiter. Defaults to |. No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Used to remove missing positions from an array.
 * 
 * @param arr      Array to compact. (Required)
 * @param delim      Temporary list delimiter. Defaults to |. (Optional)
 * @return Returns an array. 
 * @author M Gillespie for HOUCFUG (houcfug@yahoogroups.com) 
 * @version 1, March 2, 2007 
 */

function arrayCompact(arr) {
    var delim="|";
    if(arraylen(arguments) gt 1) {delim=arguments[2];}
    return listtoarray(arraytolist(arr,delim),delim);
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

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

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

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

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