CFLib.org – Common Function Library Project

ArrayAppendUnique(a1, val)

Last updated October 29, 2001
Download UDF

author

Craig Fisher                                      Craig Fisher

Version: 1 | Requires: ColdFusion 5 | 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:

<cfscript>
/**
* 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;
}
</cfscript>

Search CFLib.org


Latest Additions

Jose Diaz-Salcedo Jose Diaz-Salcedo added
cfRssFeed
3 day(s) ago

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

Duncan Duncan added
IsZIPUK
23 day(s) ago

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

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

Created by Raymond Camden / Design by Justin Johnson