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