CFLib.org – Common Function Library Project

arrayDiff(smallerArray, biggerArray)

Last updated April 06, 2013

author

Greg Nettles

Version: 2 | Requires: CF6 | Library: DataManipulationLib

Description:
This function compares 2 one-dimensional arrays (smallerArray & biggerArray) and returns an array with the values found in biggerArray but not in smallerArray.

Return Values:
Returns an array.

Example:

<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:

/**
 * Compares two arrays and returns the difference between the two.
 * v1.0 by Greg Nettles
 * v2.0 by Adam Cameron under guidance from Jeff Coughlin - replaced unnecessary/ineffcient logic
 * 
 * @param smallerArray      First array. (Required)
 * @param biggerArray      Second array. (Required)
 * @return Returns an array. 
 * @author Greg Nettles (gregnettles@gmail.com) 
 * @version 2.0, April 5, 2013 
 */
function arrayDiff(smallerArray, biggerArray) {
    biggerArray.removeAll(smallerArray);
    return biggerArray;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson