118
Chapter 5: Using Arrays and Structures
To discover whether a specific Structure contains data, use the
StructIsEmpty
function, as
follows:
StructIsEmpty(
structure_name
)
This function returns True if the structure is empty, and False if it contains data.
Finding a specific key and its value
To determine whether a specific key exists in a structure, use the
StructKeyExists
function, as
follows:
StructKeyExists(
structure_name, "key_name"
)
Do
not
put the name of the structure in quotation marks, but you do put the key name in
quotation marks. For example, the following code displays the value of the MyStruct.MyKey only
if it exists:
<cfif StructKeyExists(myStruct, "myKey")>
<cfoutput> #mystruct.myKey#</cfoutput><br>
</cfif>
You can use the
StructKeyExists
function to dynamically test for keys by using a variable to
represent the key name. In this case, you do not put the variable in quotes. For example, the
following code loops through the records of the GetEmployees query and tests the myStruct
structure for a key that matches the query’s LastName field. If ColdFusion finds a matching key, it
displays the Last Name from the query and the corresponding entry in the structure.
<cfloop query="GetEmployees">
<cfif StructKeyExists(myStruct, LastName)>
<cfoutput>#LastName#: #mystruct[LastName]#</cfoutput><br>
</cfif>
</cfloop>
If the name of the key is known in advance, you can also use the ColdFusion
IsDefined
function, as follows:
IsDefined("
structure_name
.
key
")>
However, if the key is dynamic, or contains special characters, you must use the
StructKeyExists
function.
Note:
Using
StructKeyExists
to test for the existence of a structure entry is more efficient than
using
IsDefined
. ColdFusion scopes are available as structures and you can improve efficiency by
using
StructKeyExists
to test for the existence of variables.
Getting a list of keys in a structure
To get a list of the keys in a CFML structure, you use the
StructKeyList
function, as follows:
<cfset temp=StructKeyList(
structure_name,
[
delimiter
] )>
You can specify any character as the delimiter; the default is a comma.
Use the
StructKeyArray
function to returns an array of keys in a structure, as follows:
<cfset temp=StructKeyArray(
structure_name
)>
Note:
The
StructKeyList
and
StructKeyArray
functions do not return keys in any particular
order. Use the
ListSort
or
ArraySort
functions to sort the results.
Summary of Contents for COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......