How do you return the value of a dynamic structure key?

If you need to return the value of a structure key where the key is dynamic, you must use bracket notation. Assume myStruct is a structure and you want to get the value of the key stored in a variable, keyname.

<cfset keyname = "foo">
<cfset value = myStruct[keyname]>

If the value of mystruct.foo was "Jacob", the variable value will be "Jacob."

Bracket notation should also be used when key names have a space or other invalid character. If myStruct have a key called "Raymond Camden", you would not be able to do this:

<cfoutput>
#myStruct.raymond camden#
</cfoutput>

Instead, bracket notation must be used:

<cfoutput>
#myStruct["raymond camden"]#
</cfoutput>

This question was written by Ben Forta
It was last updated on January 25, 2006.

Categories

Data Structures

Comments

comments powered by Disqus