CFLib.org – Common Function Library Project

ArrayAppendUnique(a1, val)

Last updated October 29, 2001

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

 
Rated 3 time(s). Average Rating: 4.0

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:

view plain print about
<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:

view plain print about
<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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
11 day(s) ago

Will Belden Will Belden added
longTime
17 day(s) ago

James Sleeman James Sleeman added
quickSort
27 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
30 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson