CFLib.org – Common Function Library Project

structGetKey(theStruct, theKey, defaultValue)

Last updated September 09, 2008

author

Adam Tuttle

Version: 0 | Requires: CF5 | Library: DataManipulationLib

Description:
Returns a key value from the given struct, or a default value.

Return Values:
Returns a value.

Example:

<cfset myData = structNew()>
<cfset mydata.keyThatExists = 1>

<cfset result1 = structGetKey(myData, "keyThatExists", "not found") /> <!--- this returns the value of myData.keyThatExists --->
<cfset result2 = structGetKey(myData, "keyThatNOTExists", "not found") /> <!--- this returns "not found" --->
<cfoutput>#result1#<br />#result2#</cfoutput>

Parameters:

Name Description Required
theStruct The structure. Yes
theKey Key name. Yes
defaultValue Default value to use if key does not exist. Yes

Full UDF Source:

/**
 * Returns a key value from the given struct, or a default value.
 * 
 * @param theStruct      The structure. (Required)
 * @param theKey      Key name. (Required)
 * @param defaultValue      Default value to use if key does not exist. (Required)
 * @return Returns a value. 
 * @author Adam Tuttle (j.adam.tuttle@gmail.com) 
 * @version 0, September 9, 2008 
 */
function structGetKey(theStruct, theKey, defaultVal){
    if (structKeyExists(arguments.theStruct, arguments.theKey)){
        return arguments.theStruct[arguments.theKey];
    }else{
        return arguments.defaultVal;
    }
}

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