CFLib.org – Common Function Library Project

structWrite(structure, key, value[, allowOverwrite])

Last updated May 09, 2003

author

Anthony Cooper

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
A replacement for structInsert() function. Works around the structInsert() feature of thowing an error if you are writing to a struct, the key exists, and you don't want to overwrite.

Return Values:
Returns yes or no indicating if a change was made.

Example:

<cfset fishStock = structNew()>
<cfdump var="#fishStock#" />

<cfset result = structWrite( fishStock, "trout", "100", "false" )>
<cfdump var="#result#" />
<cfdump var="#fishStock#" />
<hr />
<cfset result = structWrite( fishStock, "trout", "50", "false" )>
<cfdump var="#result#" />
<cfdump var="#fishStock#" />
<hr />
<cfset result = structWrite( fishStock, "trout", "0", "true" )>
<cfdump var="#result#" />
<cfdump var="#fishStock#" />
<hr />
<cfset result = structWrite( fishStock, "salmon", "10", "true" )>
<cfdump var="#result#" />
<cfdump var="#fishStock#" />

Parameters:

Name Description Required
structure Struct to be modified. Yes
key Key to modify. Yes
value Value to use. Yes
allowOverwrite Boolean. If false and key exists, value will not be changed. Defaults to false. No

Full UDF Source:

/**
 * Like structInsert() but does not throw an error if the key exists and you choose not to overwrite.
 * 
 * @param structure      Struct to be modified. (Required)
 * @param key      Key to modify. (Required)
 * @param value      Value to use. (Required)
 * @param allowOverwrite      Boolean. If false and key exists, value will not be changed. Defaults to false. (Optional)
 * @return Returns yes or no indicating if a change was made. 
 * @author Anthony Cooper (ant@bluevan.co.uk) 
 * @version 1, May 9, 2003 
 */
function structWrite(structure, key, value) {
    var valueWritten = false;
    var allowOverwrite = false;
        
    if(arrayLen(arguments) gte 4) allowOverwrite = arguments[4];

    if ( structKeyExists( structure, key ) IMP allowOverwrite ) {
        valueWritten = structInsert( structure, key, value, allowOverwrite );
    }
    return yesNoFormat(valueWritten);
}

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