CFLib.org – Common Function Library Project

arrayDiff(smallerArray, biggerArray)

Last updated March 13, 2007
Download UDF

author

Greg Nettles                                      Greg Nettles

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

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:

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

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

Search CFLib.org


Latest Additions

Raymond Compton Raymond Compton added
structBlend
19 day(s) ago

Duncan Duncan added
IsZIPUK
19 day(s) ago

Todd Sharp Todd Sharp added
getTagContentAll
25 day(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Created by Raymond Camden / Design by Justin Johnson