CFLib.org – Common Function Library Project

arrayDiff(smallerArray, biggerArray)

Last updated March 13, 2007

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

 
Rated 0 time(s). Average Rating: 0

Description:
This function compares 2 one-demensional arrays (a & b) with simple values and returns an array with the values found in one array (a) but not in the other (b).

Return Values:
Returns an array.

Example:

view plain print about
<cfscript>
aSmaller = arrayNew(1);
aSmaller[1] = 'a';
aSmaller[2] = 'b';
aBigger = arrayNew(1); 
aBigger[1] = 'a';
aBigger[2] = 'c';
aBigger[3] = 'b';
aBigger[4] = 'd';
</cfscript>

Smaller Array:
<cfdump var="#aSmaller#"><br />
Bigger Array:
<cfdump var="#aBigger#"><br />
Difference between the two:
<cfset aDifferences = arrayDiff(aSmaller,aBigger)>
<Ccfdump var="#aDifferences#">

Parameters:

Name Description Required
smallerArray First array. Yes
biggerArray Second array. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Compares two arrays (with simple values) and returns the difference between the two.
 * 
 * @param smallerArray      First array. (Required)
 * @param biggerArray      Second array. (Required)
 * @return Returns an array. 
 * @author Greg Nettles (gregnettles@gmail.com) 
 * @version 1, March 13, 2007 
 */

function arrayDiff(smallerArray,biggerArray) {
    var i = "";
    var result = arrayNew(1);
    var s = arrayToList(arguments.smallerArray);
    for (i=1;i lte arrayLen(arguments.biggerArray); i=i+1) if (listFind(s, arguments.biggerArray[i]) is 0) arrayAppend(result, arguments.biggerArray[i]);
    return result;
}
</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