ColdFusion supports the creation and handling of structures, which enable developers to create and maintain key-value pairs. A structure lets you build a collection of related variables that are grouped under a single name. Structures can also be used as associative arrays. You can define ColdFusion structures dynamically.
You can use structures to refer to related string values as a unit rather than individually. To maintain employee lists, for example, you can create a structure that holds personnel information such as name, address, phone number, ID number, etc. Then you can refer to this collection of information as a structure called employee rather than as a collection of individual variables.
There are three types of notation for structures:
Types of Structure Notation | |
---|---|
Notation | Description |
Objects.property | Use to refer to values in a structure. So a property, prop, of an object, obj, can be referred to as obj.prop. This notation is useful for simple assignments, as in this example:
Use this notation only when the property names (keys) are known in advance and they are strings, with no special characters, numbers, or spaces. You cannot use the dot notation when the property, or key, is dynamic. |
Associative arrays | If the key name is not known in advance, or contains spaces, numbers or special characters, you can use associative array notation. This uses structures as arrays with string indexes, for example, depts["John"] or depts["John Doe"]="Sales." See "Using Structures as Associative Arrays" for more information. |
Structure functions | The structure functions should be used when the simpler syntax styles described above cannot be used, for example when dynamic keys are required. The sections in this chapter describe how to use the structure functions. |