Chapter 9: Handling Complex Data with Structures
<CFSET rc=StructInsert(employee, "firstname", "#FORM.firstname#")>
<CFSET rc=StructInsert(employee, "lastname", "#FORM.lastname#")>
<CFSET rc=StructInsert(employee, "email", "#FORM.email#")>
<CFSET rc=StructInsert(employee, "phone", "#FORM.phone#")>
<CFSET rc=StructInsert(employee, "department", "#FORM.department#")>
Finding information in structures
To find the value associated with a specific key, use the StructFind function:
StructFind( structure_name, key )
Example
The following example shows how to generate a list of keys defined for a structure.
<CFLOOP COLLECTION=#department# ITEM="person">
<CFOUTPUT>
Key - #person#<BR>
Value - #StructFind(department,person)#<BR>
</CFOUTPUT>
Note that the StructFind function is case-insensitive. When you enumerate key-value
pairs using a loop, the keys appear in uppercase.
Getting information about structures
To find out if a given value represents a structure, use the IsStruct function:
IsStruct( variable )
This function returns TRUE if variable is a structure.
Structures are not indexed numerically, so to find out how many name-value pairs
exist in a structure, use the StructCount function, as in this example:
StructCount(employee)
To discover whether a specific Structure contains data, use the StructIsEmpty function:
StructIsEmpty( structure_name )
This function returns TRUE if the structure is empty and FALSE if it contains data.
Finding a specific key
To learn whether a specific key exists in a structure, use the StructKeyExists function.
StructKeyExists( structure_name, key )
If the name of the key is known in advance, you can use the ColdFusion function
IsDefined, as in this example:
<CFSET temp=IsDefined(" structure_name . key ")>
115
Need help?
Do you have a question about the COLDFUSION 4.5-DEVELOPING WEB and is the answer not in the manual?
Questions and answers