CFLib.org – Common Function Library Project

structGetKey(theStruct, theKey, defaultValue)

Last updated September 9, 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;
	}
}
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