CFLib.org – Common Function Library Project

arrayDiff(smallerArray, biggerArray)

Last updated April 5, 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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson