CFLib.org – Common Function Library Project

ArrayAppendUnique(a1, val)

Last updated October 29, 2001

author

Craig Fisher

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
Takes an Array and a Value as it arguments. Returns an array with the value appended if the value does not already exist within the array.

Return Values:
Returns a modified array or an error string.

Example:

<CFSCRIPT>
aX=ArrayNew(1);
aX[1]="a";
aX[2]="b";
aX[3]="c";
aX=ArrayAppendUnique(aX, "b"); //should do nothing
aX=ArrayAppendUnique(aX, "d"); //insert d at index 4
</CFSCRIPT> 
<CFDUMP var="#aX#">

Parameters:

Name Description Required
a1 The array to modify. Yes
val The value to append. Yes

Full UDF Source:

/**
 * Appends a value to an array if the value does not already exist within the array.
 * 
 * @param a1      The array to modify. 
 * @param val      The value to append. 
 * @return Returns a modified array or an error string. 
 * @author Craig Fisher (craig@altainteractive.com) 
 * @version 1, October 29, 2001 
 */
function ArrayAppendUnique(a1,val) {
    if ((NOT IsArray(a1))) {
        writeoutput("Error in <Code>ArrayAppendUnique()</code>! Correct usage: ArrayAppendUnique(<I>Array</I>, <I>Value</I>) -- Appends <em>Value</em> to the array if <em>Value</em> does not already exist");
        return 0;
    }
    if (NOT ListFind(Arraytolist(a1), val)) {
        arrayAppend(a1, val);
    }
    return a1;
}

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