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 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:

#myStruct.raymond camden#

Instead, bracket notation must be used:

#myStruct["raymond camden"]#

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


Data Structures


comments powered by Disqus